Class Inheritance - What am I doing wrong?
Virgil Dupras
hardcoded.software at gmail.com
Thu Apr 24 16:38:47 EDT 2008
On Apr 24, 10:22 pm, Brian Munroe <brian.e.mun... at gmail.com> wrote:
> 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?
Exactly, you used it wrong. It's super(B, self).
But before you start using super() everywhere, read this:
http://fuhm.net/super-harmful/
I love Python, but super() is one of those tricky things...
More information about the Python-list
mailing list