强制子类实现属性python
我试着从他那里得到答案 但是不起作用。此代码仍然通过,没有错误强制子类实现属性python,python,inheritance,properties,subclass,abstract-methods,Python,Inheritance,Properties,Subclass,Abstract Methods,我试着从他那里得到答案 但是不起作用。此代码仍然通过,没有错误 #python version: 3.8.1 from abc import ABC, abstractmethod class A(ABC): @property @abstractmethod def pr(self): return 0 class B(A): def pr(self):# not a property. return 5 b
#python version: 3.8.1
from abc import ABC, abstractmethod
class A(ABC):
@property
@abstractmethod
def pr(self):
return 0
class B(A):
def pr(self):# not a property.
return 5
b = B()
print(b.pr())
那么,我如何强制子类实现特定属性(pr如上)?代码通过时没有错误,因为您在子类中给出了抽象方法的具体实现,并且看起来(不确定)它是
abc
唯一关心的事情。此外,如果重写,则无法自动应用父类中的相同装饰器,则必须重复属性装饰器语法
我发现最好的线索是和。请仔细阅读
此外,最近有一个Python,它看起来是关闭的而没有解决(或者是重新打开?)。阅读全文也有。有趣且相关