[Tutor] subclass not inheriting attributes?

Alex Hall mehgcap at gmail.com
Tue Jan 4 02:27:04 CET 2011


On 1/3/11, Walter Prins <wprins at gmail.com> wrote:
> Sorry, my last post was too hasty.  You also had a problem calling super.
> It should be like this:
>
> class parent(object):
>  def __init__(self, l=None):
>    if l is None: self.l=[]
>    else: self.l=l
>
> class child(parent):
>  def __init__(self, *args, **kwords):
>    super(child, self).__init__(*args, **kwords)
>    self.l.append(5)
>
> c=child()
> print c.l
>
> Basically: In Python 2.x "super()" doesn't know what the current class is.
> You have to tell it, as the first parameter.  If you tell it a lie, strange
> things will happen.  That's basically what you did.  You called
> super(parent,...) instead of super(child,...)
I see, and this does indeed now work! Thanks!
>
> Have a good 2011... ;)
>
> Walter
>


-- 
Have a great day,
Alex (msg sent from GMail website)
mehgcap at gmail.com; http://www.facebook.com/mehgcap


More information about the Tutor mailing list