是。实际上,按照您的意思,实际上没有一种方法可以编写 没有
访问权限的装饰器
self。装饰的函数包装了原始函数,因此它必须至少接受该函数接受的参数(或可以从中导出这些参数的某些参数),否则它将无法将正确的参数传递给基础函数。
为此,您不需要执行任何特殊 *** 作,只需编写一个普通的装饰器即可:
def deco(func): def wrapper(self, *args, **kwargs): print "I am the decorator, I know that self is", self, "and I can do whatever I want with it!" print "I also got other args:", args, kwargs func(self) return wrapperclass Foo(object): @deco def meth(self): print "I am the method, my self is", self
然后,您可以使用它:
>>> f = Foo()>>> f.meth()I am the decorator, I know that self is <__main__.Foo object at 0x0000000002BCBE80> and I can do whatever I want with it!I also got other args: () {}I am the method, my self is <__main__.Foo object at 0x0000000002BCBE80>>>> f.meth('blah', stuff='crud')I am the decorator, I know that self is <__main__.Foo object at 0x0000000002BCBE80> and I can do whatever I want with it!I also got other args: (u'blah',) {'stuff': u'crud'}I am the method, my self is <__main__.Foo object at 0x0000000002BCBE80>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)