Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
output values change
#1
hi
int the below code:
def something(num,my_list):
    num=3
    my_list[0]=3

a=2
b=[2]
something(a,b)
print(a)
# 2 is printed
print(b)
#[3] is printed
the value of a in the final is the same as before, but the b value has changed. Why?
I read somewhere something before it, but i can not understand it. can you explain it?
thanks
Reply
#2
a and b are just names used to reference objects. The names are of no importance here.

a references an int object. Int objects are immutable, they cannot change. 2 is always 2, and you cannot change 2 to 3. You can reassign a variable to reference a different int object. num first referenced 2, then was assigned to reference 3. The only relationship between a and num is that the int object referred to by a was passed to a function as the argument num. The variables a and num know nothing about each other. Even if you rewrote the function to look like below.
def something(a,b):
    a=3
    b[0]=3
The function arguments a and b have no relationship with the global variables a and b. They just happen to have the same names.

b references a list. Lists are mutable, they can be changed. Your function changes the list object so it holds the int object 3 instead of 2.

The "value of" a and b do not change in your program. a always refers to the int object 2, and b always refers to the same list object.
akbarza likes this post
Reply
#3
(Oct-17-2023, 11:09 AM)deanhystad Wrote: a and b are just names used to reference objects. The names are of no importance here.

a references an int object. Int objects are immutable, they cannot change. 2 is always 2, and you cannot change 2 to 3. You can reassign a variable to reference a different int object. num first referenced 2, then was assigned to reference 3. The only relationship between a and num is that the int object referred to by a was passed to a function as the argument num. The variables a and num know nothing about each other. Even if you rewrote the function to look like below.
def something(a,b):
    a=3
    b[0]=3
The function arguments a and b have no relationship with the global variables a and b. They just happen to have the same names.

b references a list. Lists are mutable, they can be changed. Your function changes the list object so it holds the int object 3 instead of 2.

The "value of" a and b do not change in your program. a always refers to the int object 2, and b always refers to the same list object.

hi
good explenation and thanks, but can explain more?
thanks again
Reply
#4
Mutable vs immutable sums it all up. The mutable list object was changed. The immutable int object was not changed. If you have more questions, ask them. I am not going to guess what they might be.
akbarza likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Find a specific keyword after another keyword and change the output sgtmcc 5 856 Oct-05-2023, 07:41 PM
Last Post: deanhystad
  Rain sensor output only on change Pete6 3 2,058 May-11-2022, 10:36 PM
Last Post: Pete6
  change numerical values to categorical names JoeOpdenaker 3 2,956 Nov-02-2020, 01:32 PM
Last Post: DeaD_EyE
  change array column values without loop khalidreemy 2 3,811 May-05-2019, 09:05 AM
Last Post: DeaD_EyE
  Change Text Color Output in Python bluethundr 2 8,745 Mar-06-2019, 10:23 PM
Last Post: bluethundr
  Confusing output from 2to3 about what files need change Moonwatcher 1 4,841 Dec-30-2018, 04:07 PM
Last Post: Gribouillis
  Change output of cells. Hoogoo 2 2,627 Nov-19-2018, 12:47 AM
Last Post: Larz60+
  Change values in string multiline DavidFernandez 4 3,100 Aug-26-2018, 08:09 PM
Last Post: buran
  Change the output window size and display area issac_n 0 2,276 Apr-13-2018, 04:41 AM
Last Post: issac_n
  Change Windows Sound Output Device with Python delfar 1 10,336 Sep-15-2017, 12:11 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020