Class Variable Inheritance

Brian Jones bojo at
Thu Dec 9 01:55:09 CET 2004

I'm sure the solution may be obvious, but this problem is driving me 
mad.  The following is my code:

class a(object):

	mastervar = []

	def __init__(self):
		print 'called a'

class b(a):

	def __init__(self):
		print 'called b'

	def mapvar(self):

class c(b):

	def __init__(self):
		print 'called c'

	def mapvar(self):
		super(c, self).mapvar()

if __name__ == '__main__':

	a1 = a()
	b1 = b()
	c1 = c()
	d1 = c() # Call C again

	print a1.mastervar
	print b1.mastervar
	print c1.mastervar
	print d1.mastervar

What I don't understand is why mastervar gets modified by each _seperate 
instance_ of classes that happen to extend the base class 'a'. 
Shouldn't mastervar be contained within the scope of the inheriting 
classes?  Why is it being treated like a global variable and being 
modified by the other instances?


Brian "bojo" Jones

More information about the Python-list mailing list