Class Variable Inheritance
Brian "bojo" Jones
bojo at gvea.com
Wed Dec 8 20:49:34 EST 2004
It became clear to me that mastervar inside of class a is a static
variable, and is associated with all instances of classes that extend
class a. To get around this, I implemented a seperate mapping class:
class mapper:
def __init__(self):
self.mastermap = []
def add(self, map):
self.mastermap.append(map)
def get(self):
return self.mastermap
class a(object):
def __init__(self):
self.map = mapper()
print 'called a'
class b(a):
def __init__(self):
self.map = mapper()
print 'called b'
self.mapvar()
def mapvar(self):
self.map.add('b')
class c(b):
def __init__(self):
self.map = mapper()
print 'called c'
self.mapvar()
def mapvar(self):
super(c, self).mapvar()
self.map.add('c')
if __name__ == '__main__':
a1 = a()
a2 = a()
b1 = b()
c1 = c()
d1 = c() # Call C again
print a1.map.get()
print a1.map.get()
print b1.map.get()
print c1.map.get()
print d1.map.get()
Brian Jones wrote:
> I'm sure the solution may be obvious, but this problem is driving me
> mad. The following is my code:
>
More information about the Python-list
mailing list