help - error when trying to call super class method

Jeff McNeil jeff at jmcneil.net
Sat Sep 8 21:25:37 EDT 2007


You shouldn't even need the call to super in that method, a simple
'len(self)' ought to be sufficient.  Note that using the list object's
append method is going to be much more efficient than handling it yourself
anyways.

There are a few problems in your call to super. First, it's a callable - it
doesn't work like the Java equivalent.  Next, as the superclass of Stackx is
list, and there is no 'len' attribute on list, you'll bomb with an
AttributeError.

Lastly, you'll also generate an AttributeError when you increment 'my_len.'

Here's a quick example using 'super.'

class X(object):
    def meth(self):
        print "Hello"

class Y(X):
    def meth(self):
        super(Y, self).meth()

Y().meth()

$ python test.py
Hello

For a bit more information on the use of super, check out
http://docs.python.org/lib/built-in-funcs.html. Lastly, super will only work
with new-style classes (which yours is, due to the subclass of list).

-Jeff


On 9/8/07, dontknowwhy88 <dontknowwhy88 at yahoo.com> wrote:
>
>
> I am trying to extend list class to build a stack class -- see code
> below---
> but I got an error when I try to call len method from list class here..
> why?
> Thanks in advance!
> ---------------------
>
> class Stackx(list):
>
>   def push(self,x):
>       indx= super.len(x)
>       self.insert(my_len+1,x)
>
>   def pop(self):
>       return self[-1]
>
> def test():
>
>     myStack = Stackx([1, 2 ,3 ,4])
>     print myStack
>     myStack.push(9)
>     print myStack
>     print myStack.pop()
>
>
> if __name__=='__main__':
>     test()
>
>
> '''
>
> Traceback (most recent call last):
>   File "C:\Python25\Stack2.py", line 20, in <module>
>     test()
>   File "C:\Python25\Stack2.py", line 14, in test
>     myStack.push(9)
>   File "C:\Python25\Stack2.py", line 4, in push
>     indx= super.len(x)
> AttributeError: type object 'super' has no attribute 'len'
> '''
>
> ------------------------------
> Be a better Heartthrob. Get better relationship answers
> <http://us.rd.yahoo.com/evt=48255/*http://answers.yahoo.com/dir/_ylc=X3oDMTI5MGx2aThyBF9TAzIxMTU1MDAzNTIEX3MDMzk2NTQ1MTAzBHNlYwNCQUJwaWxsYXJfTklfMzYwBHNsawNQcm9kdWN0X3F1ZXN0aW9uX3BhZ2U-?link=list&sid=396545433>from
> someone who knows.
> Yahoo! Answers - Check it out.
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070908/167c6570/attachment.html>


More information about the Python-list mailing list