自定义NullObject中的TypeError

时间:2016-09-27 21:17:11

标签: python abstract-class typeerror

我有一个抽象类定义

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>

0 个答案:

没有答案