3

having trouble calling base class function in the following Python 2.3 script. after reviewing this post:

Call a parent class's method from child class in Python?

I've generate this small piece of code:

class Base(object):

    def func(self):
        print "Base.func"

class Derived(Base):

    def func(self):
        super(Base, self).func()
        print "Derived.func"

Derived().func()

code above generates this error:

Traceback (most recent call last):
  File "py.py", line 13, in ?
    Derived().func()
  File "py.py", line 10, in func
    super(Base, self).func()
AttributeError: 'super' object has no attribute 'func'

What am I missing?

Community
  • 1
  • 1
idanshmu
  • 5,061
  • 6
  • 46
  • 92
  • 1
    totally irrelevant, but why do you choose Python 2.3 while learning Python? – Leonardo.Z Oct 15 '13 at 07:52
  • Hi, Not learning python. just the first time I had to use super() and didn't go as planned :) anyway regarding the 2.3, that's a long story. you'll have to ask my boss. – idanshmu Oct 15 '13 at 07:54
  • 2
    But the point remains, you should definitely not be using Python 2.3 which is ten years old and unsupported. Use 2.7 or 3.3. – Daniel Roseman Oct 15 '13 at 07:56
  • As I said, it's a long story. code that was delivered to countless customers 10 years ago and it's not that easy to upgrade. long long story! thank you for caring. – idanshmu Oct 15 '13 at 07:59

1 Answers1

16

You should give super the derived class from which you want to step up, not the base class:

super(Derived, self).func()

Right now you are trying to access the func method of Base's superclass, which may not even exist.

otus
  • 5,572
  • 1
  • 34
  • 48