是否有内置功能可以打印对象的所有当前属性和值?

是否有内置功能可以打印对象的所有当前属性和值?,第1张

是否有内置功能可以打印对象的所有当前属性和值?

您实际上是将两种不同的东西混合在一起。

使用

dir()
vars()
inspect
模块来得到你所感兴趣的是(我用
__builtins__
作为一个例子,你可以使用任何对象,而不是)。

>>> l = dir(__builtins__)>>> d = __builtins__.__dict__

随心所欲地打印该词典:

>>> print l['ArithmeticError', 'AssertionError', 'AttributeError',...

要么

>>> from pprint import pprint>>> pprint(l)['ArithmeticError', 'AssertionError', 'AttributeError', 'baseException', 'DeprecationWarning',...>>> pprint(d, indent=2){ 'ArithmeticError': <type 'exceptions.ArithmeticError'>,  'AssertionError': <type 'exceptions.AssertionError'>,  'AttributeError': <type 'exceptions.AttributeError'>,...  '_': [ 'ArithmeticError',         'AssertionError',         'AttributeError',         'baseException',         'DeprecationWarning',...

交互式调试器中还可以作为命令提供漂亮的打印:

(Pdb) pp vars(){'__builtins__': {'ArithmeticError': <type 'exceptions.ArithmeticError'>,       'AssertionError': <type 'exceptions.AssertionError'>,       'AttributeError': <type 'exceptions.AttributeError'>,       'baseException': <type 'exceptions.baseException'>,       'BufferError': <type 'exceptions.BufferError'>,       ...       'zip': <built-in function zip>}, '__file__': 'pass.py', '__name__': '__main__'}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5642650.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-12-16
下一篇2022-12-16

发表评论

登录后才能评论

评论列表(0条)

    保存