在类中调用父级的__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__'
我不知道为什么,有人可以帮我吗?
问题答案:
该super
函数将 派生 类作为其第一个参数,而不是基类。
super(Person, self).__call__(someinfo)
如果需要使用基类,则可以直接进行操作(但是请注意,这会破坏多重继承,因此除非确定您要这样做,否则不应该这样做):
Parent.__call__(self, someinfo)