![](/img/trans.png)
[英]In Python, how can I get the name of the function passed to my decorator with parameters?
[英]How can I get the file name of the function that is passed to my decorator in python?
我想获取正在装饰的函数的原始文件/脚本名称等。 我怎样才能做到这一点?
def decorate(fn):
def wrapped():
return "scriptname: " + fn.scriptname?
return wrapped
我尝试使用fn.__code__
但这给了我更多我需要的东西。 我可以解析该字符串以获取函数名称,但是想知道是否有更优雅的方法来执行它
import inspect
inspect.getfile(fn)
这对于内置函数不起作用 ,你必须回到inspect.getmodule
那里。
试试这个:
return "filename: " + fn.func_code.co_filename
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.