【发布时间】:2021-04-11 13:10:07
【问题描述】:
假设我有两个类,一个继承自另一个:
class A():
def __init__(self):
pass
def doSomething(self):
print('It Works !') # Insert actual code here
class B(A):
pass
如何使doSomething 方法无法继承,以便:
(我想要让错误发生)
>>> a = A()
>>> a.doSomething()
'It Works !'
>>> b = B()
>>> b.doSomething()
Traceback (most recent call last):
File "<pyshell#132>", line 1, in <module>
b.doSomething()
AttributeError: 'B' object has no attribute 'doSomething'
【问题讨论】:
-
无法复制,对我来说很好,你在什么环境下执行?
-
为什么?这听起来与 Python 的一般精神不兼容。
-
您的代码在我的 Jupyter 笔记本上运行良好。你有哪个python版本?
-
OP 的代码正在工作,但他们希望它不工作。
-
问题不是“为什么会发生这种异常?”它实际上并没有发生。问题是“我如何让这个异常发生?”
标签: python inheritance python-3.9