How to find all unused methods of a class in PyCharm?(如何在 PyCharm 中找到一个类的所有未使用的方法?)
问题描述
我的项目中有一个名为 Article
的类.我想找到项目中未使用的所有方法.对于特定的方法,我可以按 Alt+F7
并查看它在哪里使用,如果它没有在任何地方使用,我可以安全地删除它.是否可以在不为每个方法按 Alt+F7
的情况下自动执行该过程并找到该类中所有未使用的方法?
I have a class named Article
in my project. I want to find all its methods that are unused in the project. For a particular method I can press Alt+F7
and see where it's used, and if it's not used anywhere, I can delete it safely. Is it possible to automate the process and find all methods of the class that are unused without pressing Alt+F7
for each method?
推荐答案
PyCharm 不提供此功能,因为无法可靠地确定方法未使用,因为 动态调用的方式实在太多了.
PyCharm doesn't offer this feature since it isn't possible to reliably determine that a method is unused, because there are simply too many ways to call it dynamically.
但是您可以使用 Vulture 来查找项目中的大部分死代码.参考以下命令:
But you may use Vulture to find most of dead code in a project. Refer to the following commands:
$ pip install -U vulture
$ vulture path_of_project
$ # Use --exclude for excluding particular files (e.g. virtualenv files)
$ vulture --exclude=env path_of_project
这篇关于如何在 PyCharm 中找到一个类的所有未使用的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!