[Tutor] subclass not inheriting attributes?

Vern Ceder vceder at gmail.com
Tue Jan 4 02:16:45 CET 2011


On Mon, Jan 3, 2011 at 7:47 PM, Alex Hall <mehgcap at gmail.com> wrote:

> Hi all,
> I have a solitaire game in which I use a "Pile" class. This class is
> meant to hold a bunch of cards, so I subclass it for the deck, the ace
> stacks, and the seven main stacks, defining rules and methods for each
> but also relying on the parent Pile class's methods and attributes.
> However, I keep getting an error that an attribute in the parent does
> not exist in the child. Below is a simplified example, but it gives me
> the exact same error: child object has no attribute l.
>
> class parent(object):
>  def __init__(self, l=None):
>  if l is None: l=[]
>
> class child(parent):
>  def __init__(self, *args, **kwords):
>  super(parent, self).__init__(*args, **kwords)
>

I believe you need to pass the object both to super() and to the method
itself, as in:

super(parent, self).__init__(self, *args, **kwords)

See the example at
http://docs.python.org/library/functions.html?highlight=super#super

HTH,

Vern


>  self.l.append(5)
>
> c=child()
> print c.l
>
> Again, I get an error saying that 'child' object has no attribute 'l'.
> Python 2.7 on win7x64. Thanks.
>
> --
> Have a great day,
> Alex (msg sent from GMail website)
> mehgcap at gmail.com; http://www.facebook.com/mehgcap
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>



-- 
Vern Ceder
vceder at gmail.com, vceder at dogsinmotion.com
The Quick Python Book, 2nd Ed - http://bit.ly/bRsWDW
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110103/c7dab629/attachment.html>


More information about the Tutor mailing list