确定在 python 中定义函数的文件
determine from which file a function is defined in python
我正在以编程方式打印出 python 中的函数列表。
我可以从 name
得到名字
for ifunc,func in enumerate(list_of_functions):
print(str(ifunc)+func.__name__)
如何获取定义函数的源文件名?
如果函数是对象的属性,如何获取父对象的类型?
便携性python2/3是必须的
您可以使用 __file__
变量。
print(__file__)
要获取文件名,只需使用 -
print(os.path.basename(__file__))
如果你想要完整的文件路径,你可以使用
print __file__
如此处所述https://docs.python.org/3/library/inspect.html func.__globals__
returns 定义函数的全局命名空间。
func.__module__
请问return中的模块是否定义
func.__globals__['__file__']
将return文件的整个路径defined.Only用于用户定义的函数
How to get the source filename where the function is defined ... ?
import inspect
inspect.getfile(func)
and in case the function it is attribute of a object, how to get the type of parent object?
获取方法的class(即当函数是对象的属性时):
if inspect.ismethod(func):
the_class = func.__self__.__class__
我正在以编程方式打印出 python 中的函数列表。 我可以从 name
得到名字for ifunc,func in enumerate(list_of_functions):
print(str(ifunc)+func.__name__)
如何获取定义函数的源文件名?
如果函数是对象的属性,如何获取父对象的类型?
便携性python2/3是必须的
您可以使用 __file__
变量。
print(__file__)
要获取文件名,只需使用 -
print(os.path.basename(__file__))
如果你想要完整的文件路径,你可以使用
print __file__
如此处所述https://docs.python.org/3/library/inspect.html func.__globals__
returns 定义函数的全局命名空间。
func.__module__
请问return中的模块是否定义
func.__globals__['__file__']
将return文件的整个路径defined.Only用于用户定义的函数
How to get the source filename where the function is defined ... ?
import inspect
inspect.getfile(func)
and in case the function it is attribute of a object, how to get the type of parent object?
获取方法的class(即当函数是对象的属性时):
if inspect.ismethod(func):
the_class = func.__self__.__class__