Variable Modifications in a class
Sean Ross
frobozz_electric at hotmail.com
Tue Jun 3 13:32:48 EDT 2003
"Mehta, Anish" <Anish.Mehta at enst-bretagne.fr> wrote in message
news:mailman.1054658001.11458.python-list at python.org...
> c = b
The problem is with this line. c = b makes c an alias for b, so that when
you change the value of c's attributes, you are changing the values for b's
attributes. So, when you later say
> c.a = 30
> c.b = 40
This is *roughly* equivalent to the following:
b.a = c.a = 30
b.b = c.b = 40
You see, earlier, when you created b and c with:
b = ab()
c = ab()
you bind two seperate instances of the class ab (which aliases class AB, in
your example). But, when you later *re-bind* c to b, there are no longer
referrers to two seperate instances, c and b are now bound to the same
instance. To see this, put:
print c is b
after
b = ab()
c = ab()
then put it after
c = b
an notice the results.
More information about the Python-list
mailing list