How can you find unused functions in Python code?
Question: 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
code](http://stackoverflow.com/questions/245963) and [Tool to find unused
functions in php project](http://stackoverflow.com/questions/11532), but they
seem specific to C# and PHP, respectively.

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


Answer: 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.

Alternatively, you can use static code analyzers, that just look at your code,
but don't actually run it. This has the advantage that they run much faster,
but due to python's dynamic nature the results are not 100% percent accurate
and you might want to double-check them. Two tools that come to mind here 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.

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

