在类中调用父级的__call__方法 [英] Calling parent's __call__ method within class

查看:92
本文介绍了在类中调用父级的__call__方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从继承的类中调用父级的 call 方法

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

代码看起来像这样

#!/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")

我明白了

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?

推荐答案

super函数将派生的类作为其第一个参数,而不是基类.

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)

这篇关于在类中调用父级的__call__方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆