如何在 Python 代码中找到未使用的函数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/693070/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 20:39:43  来源:igfitidea点击:

How can you find unused functions in Python code?

pythonrefactoringdead-code

提问by Brian M. Hunt

So you've got some legacy code lying around in a fairly hefty project. How can you find and delete dead functions?

因此,在一个相当庞大的项目中,您有一些遗留代码。如何查找和删除死函数?

I've seen these two references: Find unused codeand Tool to find unused functions in php project, but they seem specific to C# and PHP, respectively.

我已经看过这两个参考资料:在 php 项目中查找未使用的代码工具以查找未使用的函数,但它们似乎分别特定于 C# 和 PHP。

Is there a Python tool that'll help you find functions that aren't referenced anywhere else in the source code (notwithstanding reflection/etc.)?

是否有 Python 工具可以帮助您找到源代码中其他任何地方都没有引用的函数(尽管有反射/等)?

回答by Jendrik

In python you can find unused code by using dynamic or static code analyzers. Two examples for dynamic analyzers are coverageand figleaf. They have the drawback that you have to run all possible branches of your code in order to find unused parts, but they also have the advantage that you get very reliable results.

在 python 中,您可以使用动态或静态代码分析器找到未使用的代码。动态分析器的两个示例是coveragefigleaf。它们的缺点是您必须运行代码的所有可能分支才能找到未使用的部分,但它们也有一个优点,即您可以获得非常可靠的结果。

Alternatively, you can use static code analyzers that just look at your code, but don't actually run it. They run much faster, but due to python's dynamic nature the results are not 100% accurate so you might want to double-check them. Two tools that come to mind are pyflakesand vulture. They are complementary: Pyflakes finds unused imports and unused local variables while vulture finds unused functions, methods, classes, variables and attributes.

或者,您可以使用静态代码分析器来查看您的代码,但实际上并不运行它。它们运行得更快,但由于 python 的动态特性,结果不是 100% 准确的,因此您可能需要仔细检查它们。想到的两个工具是pyflakesvulture。它们是互补的:Pyflakes 查找未使用的导入和未使用的局部变量,而 vulture 查找未使用的函数、方法、类、变量和属性。

The tools are available at the Python Package Index http://pypi.python.org/pypi.

这些工具可在 Python 包索引http://pypi.python.org/pypi 中找到

回答by Noah

I'm not sure if this is helpful, but you might try using the coverage, figleafor other similar modules, which record which parts of your source code is used as you actually run your scripts/application.

我不确定这是否有帮助,但您可以尝试使用coveragefigleaf或其他类似模块,这些模块会记录在您实际运行脚本/应用程序时使用了源代码的哪些部分。

回答by Anonymous

pylintcan do what you want.

pylint可以做你想做的事。

回答by Stefano Borini

unless you know that your code uses reflection, as you said, I would go for a trivial grep. Do not underestimate the power of the asterisk in vim as well (performs a search of the word you have under your cursor in the file), albeit this is limited only to the file you are currently editing.

除非您知道您的代码使用反射,如您所说,否则我会选择简单的 grep。也不要低估 vim 中星号的威力(搜索文件中光标下的单词),尽管这只限于您当前正在编辑的文件。

Another solution you could implement is to have a very good testsuite (seldomly happens, unfortunately) and then wrap the routine with a deprecation routine. if you get the deprecation output, it means that the routine was called, so it's still used somewhere. This works even for reflection behavior, but of course you can never be sure if you don't trigger the situation when your routine call is performed.

您可以实施的另一个解决方案是拥有一个非常好的测试套件(不幸的是很少发生),然后用弃用例程包装该例程。如果您获得弃用输出,则意味着该例程已被调用,因此它仍在某处使用。这甚至适用于反射行为,但当然,您永远无法确定在执行例行调用时是否不会触发这种情况。

回答by Oli

Because of the fairly strict way python code is presented, would it be that hard to build a list of functions based on a regex looking for def function_name(..)?

由于 python 代码的呈现方式相当严格,基于正则表达式来构建函数列表会那么难def function_name(..)吗?

And then search for each name and tot up how many times it features in the code. It wouldn't naturally take comments into account but as long as you're having a look at functions with less than two or three instances...

然后搜索每个名称并统计它在代码中出现的次数。它自然不会考虑评论,但只要您查看少于两个或三个实例的函数......

It's a bit Spartan but it sounds like a nice sleepy-weekend task =)

这有点斯巴达,但听起来像是一个不错的周末任务 =)

回答by nir

its not only searching function names, but also all the imported packages not in use. you need to search the code for all the imported packages (including aliases) and search used functions, then create a list of the specific imports from each package (example instead of import os, replace with from os import listdir, getcwd,......)

它不仅可以搜索函数名称,还可以搜索所有未使用的导入包。您需要搜索所有导入包(包括别名)的代码并搜索使用的函数,然后从每个包创建特定导入的列表(示例而不是导入 os,替换为 from os import listdir、getcwd、... ...)