gpt4 book ai didi

python - 查找定义 python 函数的位置

转载 作者:行者123 更新时间:2023-12-01 00:10:09 29 4
gpt4 key购买 nike

我正在尝试改进一组由同事编写的 python 文件。有很多脚本,每个脚本都调用其他文件中的所有函数和类

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

有时弄清楚函数的定义位置会变得很复杂。有没有一种简单的方法可以找到函数的位置?还有它们的定义?另外,如果可能的话,变量在哪里使用?我使用 VSCode。

最佳答案

所以我一直处于这种情况,我的过程如下:

file2.py 包含诸如 from file1 import * 之类的语句的场景。

  1. In file1.py, I would copy all the import statements, and paste them in 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.
    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.
  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)
  4. Search file2.py for any eval() statements which may reference other modules, and manually import those. If no eval(), then skip.
  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.
  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!
  9. ???
  10. Profit!

这很困难,但在跟踪每个文件的使用内容和位置之后,确实值得。

关于python - 查找定义 python 函数的位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59685536/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com