Bug or Feature with (overriding) Class Variables?
Roel Mathys
rm at rm.net
Sun Nov 16 06:48:28 EST 2003
# on python 2.3.2
class foo (object) :
dict = {}
string = "foostring"
def bar(self):
self.dict["bar-key"] = "bar-value"
# is the same as
# self.dict.__setitem__( "bar-key" , "bar-value" )
# which tries to look up self.dict
# try:
# self.dict2["bar-key"] = "bar-value"
# which results in an attribute error
self.string = "bar-string"
# is adding an attribute string to the instance
# foo.string remains accessible
baz = foo()
baz.string is foo.string => True
baz.bar()
baz.string is foo.string => False
bye,
rm
More information about the Python-list
mailing list