Python中self.sender()的检测方法
Detect method by self.sender() in Python
有没有一种方法可以像使用 self.sender() 检测对象一样检测哪个方法具有 运行 其他方法?
例如,我有一个启用所有复选框的方法 A。在一个页面上我有 10 个在其他 15 个。根据将调用方法 A 的方法 B 或 C,我可以在方法 A 中定义两个场景,而不是复制代码。
是的,有办法。它利用 inspect
模块
import inspect
def echo():
"""Returns the name of a function that called it"""
return inspect.getouterframes(inspect.currentframe(), 2)[1][3]
def caller():
return echo()
print(caller(), caller.func_name)
输出:
('caller', 'caller')
有没有一种方法可以像使用 self.sender() 检测对象一样检测哪个方法具有 运行 其他方法?
例如,我有一个启用所有复选框的方法 A。在一个页面上我有 10 个在其他 15 个。根据将调用方法 A 的方法 B 或 C,我可以在方法 A 中定义两个场景,而不是复制代码。
是的,有办法。它利用 inspect
模块
import inspect
def echo():
"""Returns the name of a function that called it"""
return inspect.getouterframes(inspect.currentframe(), 2)[1][3]
def caller():
return echo()
print(caller(), caller.func_name)
输出:
('caller', 'caller')