mpe*_*pen 2 python syntax inheritance python-2.6
假设我有一个基类定义如下:
class Form(object):
class Meta:
model = None
method = 'POST'
Run Code Online (Sandbox Code Playgroud)
现在开发人员来了很长时间并定义了他的子类,如:
class SubForm(Form):
class Meta:
model = 'User'
Run Code Online (Sandbox Code Playgroud)
现在突然method属性丢失了.如何在不强迫用户从我的继承他的元类的情况下"恢复它"?我可以Form.Meta在初始化程序中或在元类的__new__func中动态添加基类吗?
只要它们不会覆盖你的__init__,或者它将被调用(即通过super),你可以修补Meta内部类:
class Form(object):
class Meta:
model = None
method = "POST"
def __init__(self, *args, **kwargs):
if self.__class__ != Form:
self.Meta.__bases__ += (Form.Meta,)
# other __init__ code here.
class SubForm(Form):
class Meta:
model = 'User'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1804 次 |
| 最近记录: |