如何在Python代码中找到未使用的函数? [英] How can you find unused functions in Python code?

查看:169
本文介绍了如何在Python代码中找到未使用的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,在一个相当庞大的项目中,您会得到一些遗留代码。您如何找到和删除无效函数?

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

我已经看到了这两个引用:查找未使用的代码在php项目中查找未使用函数的工具,但它们似乎分别针对C#和PHP。

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

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

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中,您可以使用动态或静态代码分析器查找未使用的代码。动态分析器的两个示例是 coverage figleaf 。它们的缺点是您必须运行代码的所有可能分支才能找到未使用的部分,但是它们也具有获得非常可靠的结果的优点。

In python you can find unused code by using dynamic or static code analyzers. Two examples for dynamic analyzers are coverage and 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的动态特性,结果并非100%准确,因此您可能需要仔细检查它们。
想到的两个工具是 pyflakes vulture 。它们是互补的:Pyflakes查找未使用的导入和未使用的局部变量,而Vulture查找未使用的函数,方法,类,变量和属性。

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 pyflakes and vulture. They are complementary: Pyflakes finds unused imports and unused local variables while vulture finds unused functions, methods, classes, variables and attributes.

该工具可在Python软件包索引中找到 http://pypi.python.org/pypi

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

这篇关于如何在Python代码中找到未使用的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆