在类中调用父类的 __call__ 方法
我想从继承的类中调用父级的 call 方法
代码如下所示
#!/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__'
我不明白为什么,有人可以帮我吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
super
函数将派生 类作为其第一个参数,而不是基类。如果您需要使用基类,可以直接执行(但要注意,这会破坏多重继承,因此除非您确定这就是您想要的,否则不应该这样做):
The
super
function takes the derived class as its first parameter, not the base class.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):