【发布时间】:2020-05-24 07:27:58
【问题描述】:
我是 Python 新手,我在网上阅读了很多调用超类方法子类的示例,但没有一个对我有用,请找到我的包结构
F (director)
---1. __init__.py
---2.A.py
---3.B.py
内容如下
1__init__.py
is empty
2.A.py
class A:
def hi(self,name):
print ("hi",name)
3.B.py
from F.A import A
class B(A):
def testhi(self,name):
super().hi(name)
super(B,self).hi(name)
super(self).hi(name)
A.hi(self,name)
x= B()
x.testhi("python")
它们都不起作用我总是收到错误消息'super' object has no attribute 'hi'
【问题讨论】:
-
正在发生的其他事情,您没有向我们展示。大多数调用超类方法的尝试都可以正常工作。 (不过,
super(self).hi(name)是错误的。) -
(1) 修复显示代码的缩进。 (2) 检查是否导入了正确的
A。例如。在导入A后直接在B.py中尝试print(A.hi)。 -
感谢@MichaelButscher 修复缩进解决了这个问题。发布答案,以便对某人有所帮助。
-
修复了缩进
标签: python inheritance multiple-inheritance