Re: [問題] decorator class in a class

看板Python作者 (阿寬)時間1年前 (2022/11/18 15:14), 編輯推噓0(004)
留言4則, 1人參與, 1年前最新討論串2/5 (看更多)
※ 引述《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 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 133.51.216.27 (日本) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1668755694.A.ECC.html

11/18 15:51, 1年前 , 1F
抱歉,我沒說清楚
11/18 15:51, 1F

11/18 15:52, 1年前 , 2F
對於F函式,T1, T2使用上都沒有問題
11/18 15:52, 2F

11/18 15:52, 1年前 , 3F
有問題的是class A中使用T1
11/18 15:52, 3F

11/18 15:53, 1年前 , 4F
傳進來的arg中,函少class A的self
11/18 15:53, 4F
文章代碼(AID): #1ZTp3kxC (Python)
文章代碼(AID): #1ZTp3kxC (Python)