1

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.

3
  • 2
    Can you elaborate what you mean by namespace here? Do you mean the file which holds the class definition? Or it's contents perhaps? Commented Jan 10, 2010 at 18:27
  • 3
    This is why you should not import *. Commented Jan 10, 2010 at 19:12
  • what about modifying the namespace? Commented Jun 22, 2017 at 22:00

6 Answers 6

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

can one set new attributes using dir?
2

Try inspect.getmodule:

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

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

Lots of other cool stuff in there, too.

1 Comment

And you can use dir(inspect) after the import to find out what you can do with inspect!
2

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

Comments

1

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.

Comments

0

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.

Comments

-2

You can try :

import array
print(array.__dict__) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.