我有以下代码:
class Category(abc.ABC):
foo = {"bar"}
尽管继承自abc.ABC
,但我可以实例化一个Category
:
>>> a = Category()
>>>
虽然我希望它会引发如下错误。
>>> class Foo(abc.ABC):
... @abc.abstractmethod
... def bar(self):
... return "baz"
...
>>> f = Foo()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Can't instantiate abstract class Foo with abstract methods bar
>>>
这似乎是因为Category
没有抽象方法。如何确保尝试实例化Category
会引发异常?