我的python代码中有以下情况:
class Parent(object):
def run(self):
print "preparing for run"
self.runImpl()
print "run done"
class Child(Parent):
def runImpl(self):
print "child running"
但是,我有几代这样的“装饰器”的案例,在“runImpl”之前和之后执行不同的设置/拆卸步骤,目前我被迫在我的类中定义run()
,runImpl()
和,和.runImplSingleProcess()
Parent
Child
ChildSingleProcess
我正在寻找以下形式的解决方案:
class Parent(object):
@wrapping_child_call
def run(self, func_impl, *args, **kwargs)
print "preparing for run"
func_impl(*args, **kwargs)
print "run done"
class Child(Parent):
def run(self):
print "child running"
这样一来,Child 类几乎不需要意识到这一点。
多重继承也可能存在问题。如果 aChild
继承自Parent1
and Parent2
,老实说,我不知道正确的行为应该是什么。
有谁知道一个好的,自然的,实现这一点的方法?还是我在这里强奸设计?
谢谢
Yonatan