如何在python抽象类中创建抽象属性

2024-05-16 18:47:15 发布

您现在位置:Python中文网/ 问答频道 /正文

在下面的代码中,我创建了一个基本抽象类Base。我希望从Base继承的所有类都提供name属性,因此我将此属性设置为@abstractmethod

然后我创建了Base的一个子类,称为Base_1,它旨在提供一些功能,但仍然是抽象的。在Base_1中没有name属性,但是python会毫无错误地设置该类的对象。如何创建抽象属性?

from abc import ABCMeta, abstractmethod
class Base(object):
    __metaclass__ = ABCMeta
    def __init__(self, strDirConfig):
        self.strDirConfig = strDirConfig

    @abstractmethod
    def _doStuff(self, signals):
        pass

    @property    
    @abstractmethod
    def name(self):
        #this property will be supplied by the inheriting classes
        #individually
        pass


class Base_1(Base):
    __metaclass__ = ABCMeta
    # this class does not provide the name property, should raise an error
    def __init__(self, strDirConfig):
        super(Base_1, self).__init__(strDirConfig)

    def _doStuff(self, signals):
        print 'Base_1 does stuff'


class C(Base_1):
    @property
    def name(self):
        return 'class C'


if __name__ == '__main__':
    b1 = Base_1('abc')  

Tags: nameselfbase属性initdefpropertyclass
3条回答

由于Python 3.3一个bug被修复,这意味着property()decorator在应用于抽象方法时现在被正确地标识为抽象。

注意:顺序很重要,您必须在@abstractmethod之前使用@property

Python 3.3+:python docs):

class C(ABC):
    @property
    @abstractmethod
    def my_abstract_property(self):
        ...

Python2:python docs

class C(ABC):
    @abstractproperty
    def my_abstract_property(self):
        ...

基于上面詹姆斯的回答

def compatibleabstractproperty(func):

    if sys.version_info > (3, 3):             
        return property(abstractmethod(func))
    else:
        return abstractproperty(func)

把它当装饰用

@compatibleabstractproperty
def env(self):
    raise NotImplementedError()

Python 3.3之前,不能嵌套@abstractmethod@property

使用@abstractproperty创建抽象属性(docs)。

from abc import ABCMeta, abstractmethod, abstractproperty

class Base(object):
    # ...
    @abstractproperty
    def name(self):
        pass

代码现在引发正确的异常:

Traceback (most recent call last):
  File "foo.py", line 36, in 
    b1 = Base_1('abc')  
TypeError: Can't instantiate abstract class Base_1 with abstract methods name

相关问题 更多 >