
所以我在这里寻找的东西就像PHP的 print_r函数。这样我就可以通过查看对象的状态来调试我的脚本。
你真的把两个不同的东西混合在一起。
使用 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__'}
你想要vars()与pprint()混合:
from pprint import pprint
pprint(vars(your_object))
未经作者同意,本文严禁转载,违者必究!




近期评论