开发者

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. Therefor开发者_开发知识库e, 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.


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


Try inspect.getmodule:

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

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

Lots of other cool stuff in there, too.


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


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.


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.


You can try :

import array
print(array.__dict__) 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜