Re: [问题] decorator class in a class

楼主: lycantrope (阿宽)   2022-11-18 15:14:52
※ 引述《tsaiminghan (nahgnimiast)》之铭言:
: class T1(object):
: def __init__(self, func):
: self.func = func
: def __call__(self, *arg, **kw):
: print('T1:', locals())
: return self.func(*arg, **kw)
: def T2(func):
: def wrapper(*arg, **kw):
: print(locals())
: return func(*arg, **kw)
: return wrapper
讲错还请高手指正
@T1
def F():
pass
把decorator还原:
def F():
pass
F = T1(F)
F已经变成有implement call的 T1 class instance
所以在呼叫F(),locals()会带有self
作者: tsaiminghan (tsaiminghan)   2022-11-18 15:51:00
抱歉,我没说清楚对于F函式,T1, T2使用上都没有问题有问题的是class A中使用T1传进来的arg中,函少class A的self

Links booklink

Contact Us: admin [ a t ] ucptt.com