首先,最明显的是不要在父类中做任何事情。然后,在阅读时,您只会得到一个属性错误:
AttributeError: Child instance has no attribute 'abstract_variable'
或者在父类中,您可以在每个子类中都有一个使用getter和setter
property引发
NotImplementedError并
覆盖它的
property。或在子类中将值设置为
None在类主体中…
但是,如果要提高
NotImplementedError,则可以创建一个 非数据 描述符(即, 没有
的描述符类 ,始终具有)。这使您可以在子类中设置值。
__
__set__``property``__set__
最简单的方法是
class abstract_attribute(object): def __get__(self, obj, type): raise NotImplementedError("This attribute was not set in a subclass")
你像这样使用它
class Parent(object): variable = abstract_attribute()class Child(Parent): def __init__(self): try: print(self.variable) except Exception as e: print("Got error %s" % e) self.variable = 42 print(self.variable)Child()
哪个输出
Got error This attribute was not set in a subclass42
A
property不能像my那样容易地设置值
abstract_attribute。
但是,等等,我们可以使其更加神奇:描述符可以找出从 哪个 属性访问它:
class abstract_attribute(object): def __get__(self, obj, type):# Now we will iterate over the names on the class, # and all its superclasses, and try to find the attribute # name for this descriptor # traverse the parents in the method resolution order for cls in type.__mro__: # for each cls thus, see what attributes they set for name, value in cls.__dict__.items(): # we found ourselves here if value is self: # if the property gets accessed as Child.variable, # obj will be done. For this case # If accessed as a_child.variable, the class Child is # in the type, and a_child in the obj. this_obj = obj if obj else type raise NotImplementedError( "%r does not have the attribute %r " "(abstract from class %r)" % (this_obj, name, cls.__name__)) # we did not find a match, should be rare, but prepare for it raise NotImplementedError( "%s does not set the abstract attribute <unknown>", type.__name__)
使用此代码,访问
self.variable会引发异常提示信息:
NotImplementedError: <__main__.Child object at 0x7f7c7a5dd860> does nothave the attribute 'variable' (abstract from class 'Parent')
并
Child.variable给
NotImplementedError: <class '__main__.Child'> does not have the attribute 'variable' (abstract from class 'Parent')
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)