Calling parent's __call__ method within class

瘦欲@ 提交于 2019-12-04 01:06:14

问题


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?


回答1:


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)


来源:https://stackoverflow.com/questions/7806593/calling-parents-call-method-within-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!