Mert Nuhoglu
Mert Nuhoglu

Reputation: 10133

How to find the namespace of a class easily in Python without using an IDE?

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.

Upvotes: 1

Views: 4619

Answers (6)

Sid PubgM
Sid PubgM

Reputation: 1

You can try :

import array
print(array.__dict__) 

Upvotes: -2

jathanism
jathanism

Reputation: 33716

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

Upvotes: 3

ars
ars

Reputation: 123518

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).

Upvotes: 2

Ryan Ginstrom
Ryan Ginstrom

Reputation: 14121

Try inspect.getmodule:

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

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

Lots of other cool stuff in there, too.

Upvotes: 2

Mark Tolonen
Mark Tolonen

Reputation: 177715

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.

Upvotes: 1

WildSeal
WildSeal

Reputation: 51

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.

Upvotes: 0

Related Questions