class 属性 没有进入继承 class 的实例?
A class property does not make it to the instance of the inheriting class?
我已经定义了 class 属性,方法是在元 class 中定义一个带有 @property
装饰器的属性,如本 SO answer 中所提议的那样。
因为我只能访问 class 定义(将从 metaclass 继承)定义后,我让它在之后继承,这要归功于中给出的技巧上面提到的SO答案。
我希望定义的 class 属性 在继承 class 及其实例中都可用。
但是我没有成功地在它的实例中拥有它。
那么,让我们回顾一下我当前的代码:
class TopLevel(type):
""" TopLevel metaclass defining class properties. """
@property
def depth(cls) -> int:
return cls._depth
class OtherClass:
""" Class that will inherit from 'TopLevel' after it has been defined.
Dummy implementation."""
def __init__(self, var):
self.var = var
# Making 'Otherclass' inheriting from metaclass 'TopLevel' to have available
# as class properties 'depth'.
# As per
OtherClass = TopLevel(OtherClass.__name__,
OtherClass.__bases__,
dict(OtherClass.__dict__))
# Set a value to '_depth'
OtherClass._depth = 3
# Check OtherClass has 'depth' class property
OtherClass.depth
Out[6]: 3
# Instanciate OtherClass: instance has no 'depth' attribute?
otherclass = OtherClass(4)
otherclass.depth
Traceback (most recent call last):
File "/tmp/ipykernel_748533/2343605532.py", line 1, in <module>
otherclass.depth
AttributeError: 'OtherClass' object has no attribute 'depth'
这能实现吗?
编辑
说明我对@martineau' 第一条评论的回答的代码片段:
class A:
a=2
class B(A):
def __init__(self,var):
self.var = var
b = B(42)
b.a
Out[26]: 2
b.var
Out[27]: 42
解决方法如下:
class TopLevel(type):
""" TopLevel metaclass defining class properties. """
@property
def depth(cls) -> int:
return cls._depth
class OtherClass:
""" Class that will inherit from 'TopLevel' after it has been defined.
Dummy implementation."""
def __init__(self, var):
self.var = var
# explicitely add property to OtherClass.__dict__
d = dict(OtherClass.__dict__)
d.update({"depth": TopLevel.depth})
OtherClass = TopLevel(OtherClass.__name__,
OtherClass.__bases__,
d)
OtherClass._depth = 3
otherclass = OtherClass(4)
print(otherclass.depth)
我已经定义了 class 属性,方法是在元 class 中定义一个带有 @property
装饰器的属性,如本 SO answer 中所提议的那样。
因为我只能访问 class 定义(将从 metaclass 继承)定义后,我让它在之后继承,这要归功于中给出的技巧上面提到的SO答案。
我希望定义的 class 属性 在继承 class 及其实例中都可用。 但是我没有成功地在它的实例中拥有它。
那么,让我们回顾一下我当前的代码:
class TopLevel(type):
""" TopLevel metaclass defining class properties. """
@property
def depth(cls) -> int:
return cls._depth
class OtherClass:
""" Class that will inherit from 'TopLevel' after it has been defined.
Dummy implementation."""
def __init__(self, var):
self.var = var
# Making 'Otherclass' inheriting from metaclass 'TopLevel' to have available
# as class properties 'depth'.
# As per
OtherClass = TopLevel(OtherClass.__name__,
OtherClass.__bases__,
dict(OtherClass.__dict__))
# Set a value to '_depth'
OtherClass._depth = 3
# Check OtherClass has 'depth' class property
OtherClass.depth
Out[6]: 3
# Instanciate OtherClass: instance has no 'depth' attribute?
otherclass = OtherClass(4)
otherclass.depth
Traceback (most recent call last):
File "/tmp/ipykernel_748533/2343605532.py", line 1, in <module>
otherclass.depth
AttributeError: 'OtherClass' object has no attribute 'depth'
这能实现吗?
编辑
说明我对@martineau' 第一条评论的回答的代码片段:
class A:
a=2
class B(A):
def __init__(self,var):
self.var = var
b = B(42)
b.a
Out[26]: 2
b.var
Out[27]: 42
解决方法如下:
class TopLevel(type):
""" TopLevel metaclass defining class properties. """
@property
def depth(cls) -> int:
return cls._depth
class OtherClass:
""" Class that will inherit from 'TopLevel' after it has been defined.
Dummy implementation."""
def __init__(self, var):
self.var = var
# explicitely add property to OtherClass.__dict__
d = dict(OtherClass.__dict__)
d.update({"depth": TopLevel.depth})
OtherClass = TopLevel(OtherClass.__name__,
OtherClass.__bases__,
d)
OtherClass._depth = 3
otherclass = OtherClass(4)
print(otherclass.depth)