我只想快速查看Python中对象的属性和值,如何在mac上的终端中执行此操作(非常基本的东西,从未使用过python)?
具体而言,我想看看message.attachments在这个谷歌应用程序引擎MailHandler例如(图片,视频,文档等).
Amb*_*ber 17
如果要转储整个对象,可以使用该pprint模块获得漂亮的打印版本.
from pprint import pprint
pprint(my_object)
# If there are many levels of recursion, and you don't want to see them all
# you can use the depth parameter to limit how many levels it goes down
pprint(my_object, depth=2)
Run Code Online (Sandbox Code Playgroud)
编辑:我可能误读了'对象'你的意思 - 如果你想查看类实例,而不是像dicts这样的基本数据结构,你可能想要查看inspect模块.
aar*_*ing 13
使用模块的getmembers属性inspect
它将返回(key, value)元组列表.它从obj.__dict__if if 获取值,如果getattr没有相应的条目则使用obj.__dict__.它可以帮助您免于为此目的编写几行代码.
| 归档时间: |
|
| 查看次数: |
5553 次 |
| 最近记录: |