我有一个抽象类定义
from abc import ABCMeta, abstractmethod
class AbstractClass(object):
__metaclass__ = ABCMeta
@abstractmethod
def method_a(self):
pass
我有一个从抽象类扩展的类
class NullObject(object):
def __getattr__(self, name):
return lambda *args, **kwargs: None
class MyClass(AbstractClass):
logger = NullObject()
def method_a(self):
return None
如果我尝试初始化MyClass
,那么我得到:
>>> a = MyClass()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: Can't instantiate abstract class MyClass with abstract methods logger
如果我将object()
而不是NullObject()
分配给logger
,则问题会解决,但我想了解NullObject
导致TypeError
的原因。< / p>