我在 Python 中有一个问题,我找不到任何干净的解决方案......
在调用某些方法时,我想在方法执行之前和之后执行一些代码。为了(以及许多其他事情)自动设置和清理context
变量。
为了实现这一点,我声明了以下元类:
class MyType(type):
def __new__(cls, name, bases, attrs):
#wraps the 'test' method to automate context management and other stuff
attrs['test'] = cls.other_wrapper(attrs['test'])
attrs['test'] = cls.context_wrapper(attrs['test'])
return super(MyType, cls).__new__(cls, name, bases, attrs)
@classmethod
def context_wrapper(cls, operation):
def _manage_context(self, *args, **kwargs):
#Sets the context to 'blabla' before the execution
self.context = 'blabla'
returned = operation(self, *args, **kwargs)
#Cleans the context after execution
self.context = None
return returned
return _manage_context
@classmethod
def other_wrapper(cls, operation):
def _wrapped(self, *args, **kwargs):
#DO something with self and *args and **kwargs
return operation(self, *args, **kwargs)
return _wrapped
这就像一个魅力:
class Parent(object):
__metaclass__ = MyType
def test(self):
#Here the context is set:
print self.context #prints blabla
但是,一旦我想子类Parent
化,就会出现问题,当我使用以下方法调用父方法时super
:
class Child(Parent):
def test(self):
#Here the context is set too
print self.context #prints blabla
super(Child, self).test()
#But now the context is unset, because Parent.test is also wrapped by _manage_context
#so this prints 'None', which is not what was expected
print self.context
我曾考虑在将上下文设置为新值之前保存上下文,但这只能部分解决问题......
确实,(等等,这很难解释),父方法被调用,包装器被执行,但它们接收*args
并**kwargs
寻址到Parent.test
, whileself
是一个Child
实例,所以self
如果我想用*args
and **kwargs
(例如用于自动验证目的),例如:
@classmethod
def validation_wrapper(cls, operation):
def _wrapped(self, *args, **kwargs):
#Validate the value of a kwarg
#But if this is executed because we called super(Child, self).test(...
#`self.some_minimum` will be `Child.some_minimum`, which is irrelevant
#considering that we called `Parent.test`
if not kwarg['some_arg'] > self.some_minimum:
raise ValueError('Validation failed')
return operation(self, *args, **kwargs)
return _wrapped
所以基本上,为了解决这个问题,我看到了两个解决方案:
防止在调用方法时执行包装器
super(Child, self)
有一个
self
总是“正确”类型的
这两种解决方案对我来说似乎都是不可能的......有人知道如何解决这个问题吗?一条建议 ?