How do I look inside a Python object?

后端 未结 22 1652
感情败类
感情败类 2020-12-04 04:18

I\'m starting to code in various projects using Python (including Django web development and Panda3D game development).

To help me understand what\'s going on, I wo

22条回答
  •  渐次进展
    2020-12-04 05:06

    Others have already mentioned the dir() built-in which sounds like what you're looking for, but here's another good tip. Many libraries -- including most of the standard library -- are distributed in source form. Meaning you can pretty easily read the source code directly. The trick is in finding it; for example:

    >>> import string
    >>> string.__file__
    '/usr/lib/python2.5/string.pyc'
    

    The *.pyc file is compiled, so remove the trailing 'c' and open up the uncompiled *.py file in your favorite editor or file viewer:

    /usr/lib/python2.5/string.py
    

    I've found this incredibly useful for discovering things like which exceptions are raised from a given API. This kind of detail is rarely well-documented in the Python world.

提交回复
热议问题