Bug or Feature with (overriding) Class Variables?

Eric Baker google.com.112139 at satilla.com
Sun Nov 16 11:29:19 EST 2003


Thank you Peter and Roel,

I don't believe this stumped me for hours. I guess that happens if you stay
up too late.

Basically what it boils down to, is that the operater "=" is doing different
things.

With the expression:
self.dict["bar-key"] = "bar-value"
You are modifying an existing instance of dict.

Wheras with this experession:
self.string = "bar-string"
You are actually creating a new instance, because strings are immutable the
"=" operater does not modify the string but actually creates a new one
within the current scope.

self.string = "bar-string"
 is actually
self.string = str("bar-string")

and

self.dict["bar-key"] = "bar-value"
is actually
self.dict.__setitem__("bar-key" , "bar-value" )

Thanks for your help.






More information about the Python-list mailing list