Python Updating Int And List With Function
I am new at Python and wonder how a function can act on variables and collections. I do not understand why my update_int function cannot update an int whereas update_list can? def
Solution 1:
Because changing a mutable object like list
within a function can impact the caller and its not True for immutable objects like int
.
So when you change the alist
within your list you can see the changes out side of the functions too.But note that it's just about in place changing of passed-in mutable objects, and creating new object (if it was mutable) doesn't meet this rules!!
>>>defupdate_list(alist):... alist=[3,2]...>>>l=[5]>>>update_list(l)>>>l
[5]
For more info read Naming and Binding In Python
Post a Comment for "Python Updating Int And List With Function"