假设您有两个类X&Y。您希望通过向类添加属性来修饰这些类,以生成新的类X1和Y1。在
例如:class X1(X):
new_attribute = 'something'
class Y1(Y):
new_attribute = 'something'
新的属性对于X1和Y1始终是相同的。X&Y没有任何有意义的关联,只是不可能有多重继承。还有一组其他属性,但这是退化的来说明。在
我觉得我太复杂了,但我想用一个装饰师,有点像:
^{pr2}$
我觉得我错过了一个相当普遍的模式,我将非常感谢你的想法、意见和反馈。在
谢谢你的阅读。在
布莱恩
编辑:示例:
这里有一个相关的摘录,可能会有所启发。常见类别如下: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 { it: ... }
其他时间:Thing { it: ..., users_permitted:..., owner:... }
我一直搞不懂为什么东西有时会有它的-AccessParent属性,而有时却没有。在