考虑两个类,每个类都有一个现有的共享函数,但继承路径不同:
class ClazzA(SuperClazzX):
def foo(self):
return goo(super(SuperClazzX, self).foo())
class ClazzB(SuperClazzY):
def foo(self):
return goo(super(SuperClazzY, self).foo())
foo
显然是一个可以提取到mixin的通用函数,正确的方法是什么,这样即使调用不同的super foo
,功能仍然存在?在
编辑:我删除了另一个mixin,它是混乱和无关的。在
编辑:更简单的代码
Mixin可以访问(future)子类的其他基,这里}的另一个基,即{}。Authoritative explanation here。在
C::Mixin::foo
可以访问{它的作用是:
self
是C
的实例,它的{super(Mixin, self)
时,结果对象将保留bases(Base,).foo
属性时,此对象在Base中找到它Base.foo
是用原始的self
调用的如果您希望对实现进行自定义控制,您可以访问自己的基础,例如:
^{pr2}$你的mixin可以看起来像这样,超级手动方式:
相关问题 更多 >
编程相关推荐