简体   繁体   English

查找 Python 函数的定义位置

[英]Find where a python function is defined

I am trying to improve a set of python files written by a co-worker.我正在尝试改进同事编写的一组 python 文件。 There are many scripts and each script calls all the functions and classes from other files有很多脚本,每个脚本调用其他文件中的所有函数和类

from file1.py import *
from file2.py import *
...

And at times it gets complicated to figured out where the functions are defined.有时,弄清楚函数的定义位置会变得很复杂。 Is there a short way to find the location of functions?有没有找到函数位置的捷径? And also their definitions?还有他们的定义? Also, if possible, where a variable is being used?另外,如果可能的话,在哪里使用变量? I use VSCode.我使用 VSCode。

So I've been in this exact scenario, and my process was as follows:所以我一直在这个确切的场景中,我的过程如下:

Scenario where file2.py includes statements like from file1 import * . file2.py包含诸如from file1 import *类的语句的场景。

  1. In file1.py , I would copy all the import statements, and paste them in file2.pyfile1.py 中,我将复制所有导入语句,并将它们粘贴到file2.py 中
  2. I would copy all the top-level function/class names, and set them up as comma-delimited, and replace the * in file2.py : from file1 import file1_method1, file1_method2, file1_class1 .我会复制所有顶级函数/类名称,并将它们设置为逗号分隔,并替换file2.py 中*from file1 import file1_method1, file1_method2, file1_class1
    I don't have VS Code, but I'm sure there's a way to look at the File Structure view and copy the list and reformat it into the import statement.我没有 VS Code,但我确信有一种方法可以查看文件结构视图并复制列表并将其重新格式化为导入语句。
  3. Repeat steps 1-2 for every from x import * , it's best to start from core files, and then work your way out to dependent files (reduce work for nested import * statements)对每个from x import *重复步骤 1-2,最好从核心文件开始,然后找到依赖文件(减少嵌套import *语句的工作)
  4. Search file2.py for any eval() statements which may reference other modules, and manually import those.file2.py中搜索任何可能引用其他模块的eval()语句,并手动导入它们。 If no eval() , then skip.如果没有eval() ,则跳过。
  5. Optional: Sort the import statements (yay OCD!)可选:对导入语句进行排序(是的强迫症!)
  6. Once this is done, I let the linter work, and remove all unused references (including duplicate imports) except those needed by eval , perhaps add comment for linter to ignore these.完成后,我让 linter 工作,并删除所有未使用的引用(包括重复导入),除了eval需要的那些引用,也许添加注释以让 linter 忽略这些。
  7. Run your code, and pray you didn't make mistakes (nothing to worry if you didn't!)运行你的代码,并祈祷你没有犯错(如果你没有犯错,不用担心!)
  8. Repeat 1-7 for every file in your application!对应用程序中的每个文件重复 1-7!
  9. ??? ???
  10. Profit!利润!

This has been tough, but really has been worth it after all that work to trace what is being used and where in each file.这很困难,但在跟踪正在使用的内容以及每个文件中的位置进行所有工作之后,这确实是值得的。

If you want to know where a particular function comes from, I think you can find the module using the __module__ property.如果您想知道特定函数的来源,我认为您可以使用__module__属性找到该模块。 You could use:你可以使用:

print(myFunction.__module__)

To get the file it has been imported from.获取已从中导入的文件。 You can then use:然后您可以使用:

print(myModule.__file__) 

To work out the path to the file the module is from.找出模块所在文件的路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM