![](/img/trans.png)
[英]How can I determine which variable to use for a defined function in python
[英]determine from which file a function is defined in python
我正在以編程方式打印出 python 中的函數列表。 我可以從名字中得到名字
for ifunc,func in enumerate(list_of_functions):
print(str(ifunc)+func.__name__)
如何獲取定義函數的源文件名?
如果函數是對象的屬性,如何獲取父對象的類型?
可移植性python2/3是必須的
func.__module__
將在定義的女巫中返回模塊
func.__globals__['__file__']
將返回定義它的文件的整個路徑。僅適用於用戶定義的函數
如何獲取定義函數的源文件名...?
import inspect
inspect.getfile(func)
如果函數是對象的屬性,如何獲取父對象的類型?
獲取方法的類(即當函數是對象的屬性時):
if inspect.ismethod(func):
the_class = func.__self__.__class__
要獲取文件名,只需使用 -
print(os.path.basename(__file__))
如果你想要完整的文件路徑,你可以使用
print __file__
如此處所述https://docs.python.org/3/library/inspect.html func.__globals__
返回定義函數的全局命名空間。
您可以使用__file__
變量。
print(__file__)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.