On Sat, Nov 13, 2010 at 5:46 AM, Tracubik <affdfsdfds...@b.com> wrote:
> hi all, > i've this on python 2.6.6: > > >>> def change_integer(int_value): > ... int_value = 10 > ... > ... def change_list(list): > ... list[0] = 10 > ... > ... a = 1 > ... l = [1,1,1] > ... > ... change_integer(a) > ... change_list(l) > ... > ... print a > ... print l > 1 > [10, 1, 1] > > why the integer value doesn't change while the list value do? > in Pascal i can choose the behavour of parametres, how this work on Python? > also a link could be appreciated > > Thanks in advance > Nico > -- > http://mail.python.org/mailman/listinfo/python-list > $ cat python-argument-passing #!/usr/bin/python def assign_int(x): print 'id(x) is %d' % id(x) x = 5 # x is now a totally different integer, which won't be reflected in # the caller, because id(x) has changed print 'id(x) is %d' % id(x) def assign_list(list_): # list_ is initially a copy of the reference to list_1 print 'id(list_) is %d' % id(list_) list_ = [ 'a', 'b', 'c' ] # list_ is no a reference to a totally different list, as indicated # by the change in id(). This too won't be reflected in the caller, # because id(list_) has changed print 'id(list_) is %d' % id(list_) def assign_list_element(list_): # list_ is initially a copy of the reference to list_2 print 'id(list_) is %d' % id(list_) list_[1] = 101 # list_ is still a copy of the reference to list_2 - we only changed # one thing in that list, not the list itself. This _will_ be reflected # in the caller, because we're still talking about the same list. print 'id(list_) is %d' % id(list_) x = 1 list_1 = [ 1, 2, 3 ] list_2 = [ 4, 5, 6 ] assign_int(x) print 'x after assign_int(x): %d' % x print assign_list(list_1) print 'list_1 after assign_list(list_1): %s' % list_1 print assign_list_element(list_2) print 'list_1 after assign_list_element(list_2): %s' % list_2 print print 'The author likes to think of all this as pass by value - but' print "when we pass a simple type like an int or float, we're passing" print "the object itself, while for collections of things we're still" print "passing the object itself, but that object _contains_ other things" print "which can easily be changed from the caller's perspective as well." benchbox-dstromberg:~/src/python-var-passing i686-pc-linux-gnu 10388 - above cmd done 2010 Sat Nov 13 01:26 PM $ ./python-argument-passing id(x) is 157519248 id(x) is 157519200 x after assign_int(x): 1 id(list_) is 3077596300 id(list_) is 3077683052 list_1 after assign_list(list_1): [1, 2, 3] id(list_) is 3077682092 id(list_) is 3077682092 list_1 after assign_list_element(list_2): [4, 101, 6] The author likes to think of all this as pass by value - but when we pass a simple type like an int or float, we're passing the object itself, while for collections of things we're still passing the object itself, but that object _contains_ other things which can easily be changed from the caller's perspective as well. benchbox-dstromberg:~/src/python-var-passing i686-pc-linux-gnu 10388 - above cmd done 2010 Sat Nov 13 01:26 PM HTH :)
-- http://mail.python.org/mailman/listinfo/python-list