tags:

views:

88

answers:

5

Normally, I use IntelliJ for python programming. But sometimes I don't have access to it or just to make a quick edit I open a Python file in a text editor.

At those times, it is really difficult to find the namespace of a class. I am googling it. But it takes time. Is there a better way to do this?

Edit: Looking at the responses, I noticed that my question was not very clear.

I need to find the namespace of a class at coding time, not in runtime. Therefore, introspection methods like using inspect or __module__ doesn't help me.

WildSeal suggests using online doc. This is good but it is only useful for Python's standard libraries. I want to search all the modules installed in my current Python path. This is easy with IntelliJ. It has already indexed all the files.

I tried to use grep to search for the class inside the site-packages directory. But it takes a lot of time to search all the files, probably since they have not been indexed.

A: 

You can check the online documentation, or the documentation installed with Python. If you search for a function or class, you'll get all the relevant information (I assume you meant the package or module of a class or of a function, in the Python standard library).

There aren't so many of them though, at least that people usually use at the same time, so you should quickly get to know them.

WildSeal
A: 

The special attribute __module__ is the module name in which a class was defined. It will be '__main__' if defined in the top-level script.

Mark Tolonen
+1  A: 

Let us not forget about dir(), which is heavily used by those of us who use vim as our IDE.

jathanism
A: 

Try inspect.getmodule:

>>> import inspect
>>> class Foo:
    pass

>>> inspect.getmodule(Foo)
<module '__main__' (built-in)>

Lots of other cool stuff in there, too.

Ryan Ginstrom
And you can use `dir(inspect)` after the import to find out what you can do with `inspect`!
jathanism
+1  A: 

I use ctags and I've got my site-packages as well as other folders indexed in advance. There are a number of GUI and command line tools that will integrate with ctags to do what you need. Personally, I use vim as a text editor with the TagList plugin (instructions).

ars