![](/img/trans.png)
[英]How to create a command “home” or “quit” that returns to the main function of a program in python
[英]Python command that returns location to a function
我知道这听起来很愚蠢。
我正在寻找将命令的路径返回到函数位置的python命令。 像
command(np.reshape)
应该提供np.reshape所在文件夹的路径。 我对查找整个模块的主要位置(此处为numpy)不感兴趣,已经多次回答。 我需要的功能本身的确切路径,完全像which
这样做是在MATLAB。
我相信您正在寻找inspect.getsourcefile()
根据文档,inspect.getsourcefile(object)
返回定义对象的Python源文件的名称。 如果对象是内置模块,类或函数,则将失败并显示TypeError。
这是使用join函数进行测试时得到的结果:
>>> import inspect
>>> from os.path import join
>>> inspect.getsourcefile(join)
'C:\\Users\\ankit\\...\\Python\\Python36\\lib\\ntpath.py'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.