Python 3文档提到从3.3开始不推荐使用abc.abstractproperty,而赞成@property和@abstractmethod。是否有另一种方法来实现abc.abstractproperty与Python 2和3兼容的抽象属性(不带)?
我试过了
import abc
from future.utils import with_metaclass
class Base(with_metaclass(abc.ABCMeta, object)):
@property
@abc.abstractmethod
def x(self):
raise NotImplementedError
class C(Base):
pass
C()
Run Code Online (Sandbox Code Playgroud)
TypeError: Can't instantiate abstract class C with abstract methods x在Python 3中正确引发,但在Python 2中不正确。