List of object references in python -


i'm aware question has been asked here in one form or another, none of answers address behaviour i'm seeing. i'm given understand list of objects should hold references objects. observe seems contract this:

class foo(object):     def __init__(self,val):         self.value=val  = foo(2) b = [a] print b[0].value  = foo(3) print b[0].value 

i expect see 2 printed first, 3, since expect b[0] point a, new object. instead see 2 , 2. missing here?

in python, assignment operator binds result of right hand side expression name left hand side expression.

so, when say

a = foo(2) b = [a] 

you have created foo object , refer a. create list b reference foo object (a). why b[0].value prints 2.

but,

a = foo(3) 

creates new foo object , refers name a. so, a refers new foo object not old object. list still has reference old object only. why still prints 2.


Comments