A change to a class attribute does not propagate - bug or feature?
Vesselin
vesselin at beonic.com
Thu Jan 10 02:25:06 EST 2002
""" Please run this code. It seems that in the line:
def __init__(self, Name = "newClass" + str(baseClass.ClassId)):
Python takes the value of baseClass.ClassId from somewhere else
I use ActivePython build 2.1.212
"""
class baseClass:
ClassId = 1
def __init__(self, Name):
baseClass.ClassId += 1
print "init in baseClass, new ClassId = ", baseClass.ClassId
self.ClassName = Name
class newClass(baseClass):
def __init__(self, Name = "newClass" + str(baseClass.ClassId)):
baseClass.__init__(self, Name)
if __name__ == '__main__':
print "\nPrinting the initial value of the class attribute: 1/1:"
print baseClass.ClassId
print newClass.ClassId
print "\nCreating a newClass instance"
nClass = newClass()
print "\nPrinting the current value of the class attribute: 2/2/2:"
print baseClass.ClassId
print newClass.ClassId
print nClass.ClassId
print "\nPrinting nClass.ClassName: newClass1:"
print nClass.ClassName
print "\nSo far so good. Now lets create another newClass instance"
nClass2 = newClass()
print "\nPrinting nClass2.ClassName"
print "Expected newClass2, got newClass1:"
print nClass2.ClassName
print "\nNote that the \"real\" class attribute HAS CHANGED: 3/3/3/3:"
print baseClass.ClassId
print newClass.ClassId
print nClass.ClassId
print nClass2.ClassId
#Any help will be highly appreciated
More information about the Python-list
mailing list