When studying a snippet of unknown Python code, I occasionally bump into the
varName.methodName()
pattern.
To figure out what’s this, I shall study the code more, find where varName was instantiated, find its type. So if varName proves to be an instance of ClassName class, I would knew that methodName() is a method of ClassName.
Sometimes varName == self and methodName() is a method of this class, or a method inherited from some other class, if the current class is subclassing some other classes.
Are there quick ways / tools that could take ‘methodName’ as input, scan over all installed Python modules and show which classes have methodName()?
The closest thing related to this I know of is ipython. If I type a class name, then dot (‘.’) then TAB, it can show the class members. Instead of a class I could use a name of an object (which is an instance of a certain class) and it would work too. As soon as I choose a method name from the provided options, I can type ‘?’ or ‘??’ and get some help if there’s a docstring.
I wonder if ipython can do some intelligent scanning based only on ‘methodName’ string.
If you know alternatives to ipython that could possibly help with this, please do suggest them.
Edit: as requested, I’m explicitly adding that I would like a way to find methods by method names not only in Python source code files. Some Python packages (notably PyQt) contain a lot of .so files, and ipython is able to do completions by presumably importing them first. So a plain text search like grep (or even ctags) won’t do the trick here.
You want Exuberant ctags (old ctags doesn’t generate tags for Python).
Once you’ve installed (the way to do that depends on your platform), run it on your files:
(you can run it multiple times to append tags to an existing tag file, have it recurse into subdirectories, etc, all with command-line options) and it makes a
tagsfile like the following (for brevity I’m using a single Python file with one class defining a single method):Now, quoting this article:
Also, control-P does (some attempt at) code completion for identifiers found in
tags.(
:help tagsin vim will give you more details).