【发布时间】:2020-07-20 08:44:13
【问题描述】:
什么是要求继承的最安全方法?目前我正在做类似的事情:
>>> class A: # Never instantiate by itself.
... def a(self):
... self.foo()
...
... class B(A):
... def foo(self):
... print('123')
...
... class C(A):
... def foo(self):
... print('456')
...
>>> B().a()
123
C().a()
456
>>> A().a() # Expect an error.
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "<input>", line 3, in a
AttributeError: 'A' object has no attribute 'foo'
这是最好的方法吗?
【问题讨论】:
标签: python inheritance