Class Inheritance - What am I doing wrong?

Brian Munroe brian.e.munroe at gmail.com
Thu Apr 24 16:22:53 EDT 2008


My example:

class A(object):

	def __init__(self, name):
		self.__name = name

	def getName(self):
		return self.__name

class B(A):

	def __init__(self,name=None):
		super(A,self).__init__()

	def setName(self, name):
		self.__name = name

if __name__ == '__main__':

	a = A('class a')
	print a.getName()

	b = B('class b')
	print b.getName()

	b.setName('class b, reset')
	print b.getName()

I get the following error:

mtinky:~ brian$ python teste.py
class a
Traceback (most recent call last):
  File "teste.py", line 23, in <module>
    print b.getName()
  File "teste.py", line 7, in getName
    return self.__name
AttributeError: 'B' object has no attribute '_A__name'

Am I *not* using super() correctly?  Also, did I define my the class B
constructor correctly?



More information about the Python-list mailing list