假设您有两个类 X 和 Y。您想要通过向类添加属性来生成新类 X1 和 Y1 来装饰这些类。
例如:
class X1(X):
new_attribute = 'something'
class Y1(Y):
new_attribute = 'something'
new_attribute 对于 X1 和 Y1 将始终相同。 X & Y 没有任何有意义的关系,除了不可能多重继承。还有一组其他属性,但这只是为了说明而退化。
我觉得我把它复杂化了,但我曾想过使用装饰器,有点像这样:
def _xywrap(cls):
class _xy(cls):
new_attribute = 'something'
return _xy
@_xywrap(X)
class X1():
pass
@_xywrap(Y)
class Y1():
pass
感觉好像我错过了一个相当普遍的模式,我非常感谢您的想法、意见和反馈。
感谢阅读。
布莱恩
编辑:示例:
这是一个相关的摘录,可能会有所启发。常用类如下:
from google.appengine.ext import db
# I'm including PermittedUserProperty because it may have pertinent side-effects
# (albeit unlikely), which is documented here: [How can you limit access to a
# GAE instance to the current user][1].
class _AccessBase:
users_permitted = PermittedUserProperty()
owner = db.ReferenceProperty(User)
class AccessModel(db.Model, _AccessBase):
pass
class AccessExpando(db.Expando, _AccessBase):
pass
# the order of _AccessBase/db.* doesn't seem to resolve the issue
class AccessPolyModel(_AccessBase, polymodel.PolyModel):
pass
这是一个子文档:
class Thing(AccessExpando):
it = db.StringProperty()
有时Thing会有以下属性:
Thing { it: ... }
其他时候:
Thing { it: ..., users_permitted:..., owner:... }
我一直无法弄清楚为什么 Thing 有时会有它的 _AccessParent 属性,而有时却没有。
最佳答案
使用 3 个参数 type :
def makeSomeNicelyDecoratedSubclass(someclass):
return type('MyNiceName', (someclass,), {'new_attribute':'something'})
正如您推测的那样,这确实是一个相当流行的习语。
编辑:在一般情况下,如果某个类具有自定义元类,您可能需要提取并使用它(使用 1 个参数 type
)代替 type
本身,以保留它(这可能是您的 Django 和 App Engine 模型的情况):
def makeSomeNicelyDecoratedSubclass(someclass):
mcl = type(someclass)
return mcl('MyNiceName', (someclass,), {'new_attribute':'something'})
这也适用于上面更简单的版本(因为在没有自定义元类的简单情况下 type(someclass) is type
)。
关于Python装饰一个类改变父对象类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1412210/