Python updating int and list with function -
i new @ python , wonder how function can act on variables , collections. not understand why update_int function cannot update int whereas update_list can?
def update_int(i):   = 1  = 0 update_int(i) print returns 0
def update_list(alist):   alist[0] = 1  = [0] update_list(i) print returns [1]
because changing mutable object list within function can impact caller , not true immutable objects int.
so when change alist within list can see changes out side of functions too.but note it's in place changing of passed-in mutable objects, , creating new object (if mutable) doesn't meet rules!! 
>>> def update_list(alist): ...   alist=[3,2] ...  >>> l=[5] >>> update_list(l) >>> l [5] for more info read naming , binding in python
Comments
Post a Comment