Python Class 属性和子类化
Python Class Attributes and Subclassing
我在谷歌上搜索并玩了一段时间,但没有结果尝试做这样的事情:
class A(object):
cl_att = 'I am an A class attribute'
class B(A):
cl_att += ' modified for B type'
class C(A):
cl_att += ' modified for C type'
instA = A()
print insAt.cl_att
>>> I am an A class attribute
instB = B()
print instB.cl_att
>>> I am an A class attribute modified for B type
instC = C()
print instC.cl_att
>>> I am an A class attribute modified for C type
print instA.cl_att
>>> I am an A class attribute
简而言之,我希望能够 "use and then override" 我的 parent class.
的 class 属性
引用父 class 属性并连接到它:
class A(object):
cl_att = 'I am an A class attribute'
class B(A):
cl_att = A.cl_att + ' modified for B type'
class C(A):
cl_att = A.cl_att + ' modified for C type'
Class 主体的执行很像函数,本地名称构成 class 属性。 cl_att
在新的 'function' 中不存在,无法为 B
和 C
创建主体,因此您需要直接引用基础 class 上的属性相反。
演示:
>>> class A(object):
... cl_att = 'I am an A class attribute'
...
>>> class B(A):
... cl_att = A.cl_att + ' modified for B type'
...
>>> class C(A):
... cl_att = A.cl_att + ' modified for C type'
...
>>> A.cl_att
'I am an A class attribute'
>>> B.cl_att
'I am an A class attribute modified for B type'
>>> C.cl_att
'I am an A class attribute modified for C type'
假设您只有一个 parent class,您可以使用 child class 答案末尾定义的 inherit_and_append
装饰器es,得到想要的结果,像这样:
class A(object):
cl_att = 'I am an A class attribute'
@inherit_and_append('cl_att')
class B(A):
cl_att = ' modified for B type'
@inherit_and_append('cl_att')
class C(A):
cl_att = ' modified for C type'
如果有更高级的要求或条件,您可以进一步扩展装饰器。
class inherit_and_append(object):
def __init__(self, *attrs):
super(inherit_and_append, self).__init__()
self._attrs = attrs
def __call__(self, klass):
parent = klass.__bases__[0] # Assuming you have a single parent class
for attr in self._attrs:
if not hasattr(parent, attr):
raise AttributeError("'{}' class has no '{}' attribute".format(parent.__name__, attr))
parent_value = getattr(parent, attr)
klass_value = getattr(klass, attr)
setattr(klass, attr, parent_value + klass_value)
return klass
我在谷歌上搜索并玩了一段时间,但没有结果尝试做这样的事情:
class A(object):
cl_att = 'I am an A class attribute'
class B(A):
cl_att += ' modified for B type'
class C(A):
cl_att += ' modified for C type'
instA = A()
print insAt.cl_att
>>> I am an A class attribute
instB = B()
print instB.cl_att
>>> I am an A class attribute modified for B type
instC = C()
print instC.cl_att
>>> I am an A class attribute modified for C type
print instA.cl_att
>>> I am an A class attribute
简而言之,我希望能够 "use and then override" 我的 parent class.
的 class 属性引用父 class 属性并连接到它:
class A(object):
cl_att = 'I am an A class attribute'
class B(A):
cl_att = A.cl_att + ' modified for B type'
class C(A):
cl_att = A.cl_att + ' modified for C type'
Class 主体的执行很像函数,本地名称构成 class 属性。 cl_att
在新的 'function' 中不存在,无法为 B
和 C
创建主体,因此您需要直接引用基础 class 上的属性相反。
演示:
>>> class A(object):
... cl_att = 'I am an A class attribute'
...
>>> class B(A):
... cl_att = A.cl_att + ' modified for B type'
...
>>> class C(A):
... cl_att = A.cl_att + ' modified for C type'
...
>>> A.cl_att
'I am an A class attribute'
>>> B.cl_att
'I am an A class attribute modified for B type'
>>> C.cl_att
'I am an A class attribute modified for C type'
假设您只有一个 parent class,您可以使用 child class 答案末尾定义的 inherit_and_append
装饰器es,得到想要的结果,像这样:
class A(object):
cl_att = 'I am an A class attribute'
@inherit_and_append('cl_att')
class B(A):
cl_att = ' modified for B type'
@inherit_and_append('cl_att')
class C(A):
cl_att = ' modified for C type'
如果有更高级的要求或条件,您可以进一步扩展装饰器。
class inherit_and_append(object):
def __init__(self, *attrs):
super(inherit_and_append, self).__init__()
self._attrs = attrs
def __call__(self, klass):
parent = klass.__bases__[0] # Assuming you have a single parent class
for attr in self._attrs:
if not hasattr(parent, attr):
raise AttributeError("'{}' class has no '{}' attribute".format(parent.__name__, attr))
parent_value = getattr(parent, attr)
klass_value = getattr(klass, attr)
setattr(klass, attr, parent_value + klass_value)
return klass