How do I look inside a Python object?

后端 未结 22 1699
感情败类
感情败类 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 04:57

    Try using:

    print(object.stringify())
    
    • where object is the variable name of the object you are trying to inspect.

    This prints out a nicely formatted and tabbed output showing all the hierarchy of keys and values in the object.

    NOTE: This works in python3. Not sure if it works in earlier versions

    UPDATE: This doesn't work on all types of objects. If you encounter one of those types (like a Request object), use one of the following instead:

    • dir(object())

    or

    import pprint then: pprint.pprint(object.__dict__)

提交回复
热议问题