class Worker:
def work(self, callback: Callable):
# 如何判断 callback 隐含参数 self 是自己?
pass
def rest(self):
pass
def todo():
worker = Worker()
worker.work(worker.rest)
1
anzu OP 我懂了,这样
if getattr(callback, '__self__', None) is self: print('callback is self') |
2
ranleng 2020-05-29 11:38:59 +08:00
不知道对不对.
Worker().work 的 __class__ 是 method todo 的 __class__ 是 function |