Python 2.7 是否可以对decorator和function使用相同的参数
我需要了解如何访问在decorator中传递给函数的变量 让我用一个例子来解释,是否可以这样做:Python 2.7 是否可以对decorator和function使用相同的参数,python-2.7,arguments,decorator,Python 2.7,Arguments,Decorator,我需要了解如何访问在decorator中传递给函数的变量 让我用一个例子来解释,是否可以这样做: class test(object): .... @DecoratorClass(myWrapper(self, x)) def myFunction(self, x): print x print self.y 在某个点上,会创建一个测试类的实例,并从某处调用myFunction。我需要将相同的参数路径设置为myWrapper 我希望这一点
class test(object):
....
@DecoratorClass(myWrapper(self, x))
def myFunction(self, x):
print x
print self.y
在某个点上,会创建一个测试类
的实例,并从某处调用myFunction
。我需要将相同的参数路径设置为myWrapper
我希望这一点足够清楚。装饰程序将函数替换为一个可调用函数,该函数将被封装,因此可调用函数将被传递与原始函数相同的参数。例如:
def Decorator(original_function):
def replacement_function(x):
# You now have 'x' here... do what you want with it.
return replacement_function
@Decorator
def MyFunction(x):
# ...
请注意,您需要将“myWrapper”的构造延迟到函数执行为止(因为在此之前您将没有函数参数)。谢谢,但是如果我需要参数来构造一个适当的myWrapper,应该怎么做?