10

I'd like to call a parent's call method from inherited class

Code looks like this

#!/usr/bin/env python

class Parent(object):

    def __call__(self, name):
        print "hello world, ", name


class Person(Parent):

    def __call__(self, someinfo):                                                                                                                                                            
        super(Parent, self).__call__(someinfo)

p = Person()
p("info")

And I get,

File "./test.py", line 12, in __call__
super(Parent, self).__call__(someinfo)
AttributeError: 'super' object has no attribute '__call__'

And I can't figure out why, can somebody please help me with this?

TylerH
  • 20,799
  • 66
  • 75
  • 101
Jan Vorcak
  • 19,261
  • 14
  • 54
  • 90

1 Answers1

18

The super function takes the derived class as its first parameter, not the base class.

super(Person, self).__call__(someinfo)

If you need to use the base class, you can do it directly (but beware that this will break multiple inheritance, so you shouldn't do it unless you're sure that's what you want):

Parent.__call__(self, someinfo)
Petr Viktorin
  • 65,510
  • 9
  • 81
  • 81
  • 4
    Can you also explain why `super()()` without explicit `__call__` fails for future googlers? Oh the lazyness of asking new question :-) – Ciro Santilli OurBigBook.com Dec 16 '18 at 12:12
  • 2
    `super()` instances proxy attribute access, but [the call operation doesn't look up the `__call__` attribute on the instance](https://docs.python.org/3/reference/datamodel.html#special-method-lookup). (This is mentioned [in `super()` docs](https://docs.python.org/3.5/library/functions.html#super), but the language there is quite technical…) – Petr Viktorin Feb 11 '19 at 21:19