Confused about class behavior

Opinderjit bhellao at my-deja.com
Sat Sep 16 13:34:02 EDT 2000


I don't seem to understand the following behavior. I create this class
having one attribute, color, set to 'Green'

class C:
    color = "Green"

I create two instances of class C, instance 'a' and 'b', and call the
access the color attribute. The output is just as expected.
>> a = C(); b = C()
>> a.color; b.color
'Green'
'Green'

When I do the following, both instances a and b were affected.
>> C.color = 'Red'
>> a.color; b.color
'Red'
'Red'

Is this correct? Futher more, if I change the color attribute of one of
the instance classes, b,  followed by changing the color of the base
class, I get the follwoing:
>> b.color = 'Yellow'
>> C.color = 'Blue'
>> a.color; b.color
'Blue'
'Yellow'

Because the color of instance b had already been changed, changing the
color of the Base class didn't affect instance b, but it still affected
instance a. Does instance b get a new memory address after changing the
color attribute?


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list