
您实际上是将两种不同的东西混合在一起。
使用
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__'}欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)