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

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -