这里我要找的是PHP的print_r函数。
这样我就可以通过查看有问题的对象的状态来调试脚本。
这里我要找的是PHP的print_r函数。
这样我就可以通过查看有问题的对象的状态来调试脚本。
当前回答
你实际上是把两种不同的东西混合在一起。
使用dir(), vars()或inspect模块来获取您感兴趣的内容(我使用__builtins__作为示例;你可以用任何物体代替)。
>>> l = dir(__builtins__)
>>> d = __builtins__.__dict__
随你喜欢,把那本词典打印出来吧:
>>> print l
['ArithmeticError', 'AssertionError', 'AttributeError',...
or
>>> 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',
...
Pretty print也可以在交互式调试器中作为命令使用:
(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__'}
其他回答
如果你想在一个复杂的数据结构中看到所有的值,那么可以这样做:
from pprint import pprint
pprint(my_var)
其中my_var是您感兴趣的变量。当我使用pprint(vars(my_var))时,我什么都没有得到,这里的其他答案没有帮助,或者方法看起来不必要的长。顺便说一下,在我的特殊情况下,我正在检查的代码有一个字典的字典。
值得指出的是,对于一些自定义类,您可能只会得到一个无用的<someobject。0x7f739267f400>类型的输出。在这种情况下,你可能不得不实现一个__str__方法,或尝试一些其他的解决方案。
我还发现,在我获得这种对象类型输出的一个实例中,vars()显示了我想要的结果。因此,覆盖这两种情况的更好解决方案是分别尝试两种情况。但是使用vars()有时会抛出异常,例如,TypeError: vars()参数必须具有__dict__属性。
我仍然希望找到一些简单的,在所有场景下都可以工作的东西,而不需要第三方库。
在大多数情况下,使用__dict__或dir()将获得你想要的信息。如果您碰巧需要更多的细节,标准库包括inspect模块,它允许您获得一些令人印象深刻的细节。一些真正宝贵的信息包括:
函数和方法参数的名称 类层次结构 实现一个函数/类对象的源代码 帧对象的局部变量
如果你只是在寻找“我的对象有哪些属性值?”,那么dir()和__dict__可能就足够了。如果您真的想深入研究任意对象的当前状态(请记住,在python中几乎所有东西都是对象),那么inspect值得考虑。
你实际上是把两种不同的东西混合在一起。
使用dir(), vars()或inspect模块来获取您感兴趣的内容(我使用__builtins__作为示例;你可以用任何物体代替)。
>>> l = dir(__builtins__)
>>> d = __builtins__.__dict__
随你喜欢,把那本词典打印出来吧:
>>> print l
['ArithmeticError', 'AssertionError', 'AttributeError',...
or
>>> 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',
...
Pretty print也可以在交互式调试器中作为命令使用:
(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__'}
无论你的变量在类中是如何定义的,在__init__内部还是外部,这都是有效的。
your_obj = YourObj()
attrs_with_value = {attr: getattr(your_obj, attr) for attr in dir(your_obj)}
也许值得一查——
Python中是否存在与Perl的Data:: dump相同的程序?
我的建议是——
https://gist.github.com/1071857
注意,perl有一个名为Data::Dumper的模块,它将对象数据转换回perl源代码(注意:它不将代码转换回源代码,而且几乎总是你不想在输出中使用对象方法函数)。这可以用于持久性,但常见的目的是用于调试。
标准的python pprint有很多事情无法实现,特别是当它看到一个对象的实例时,它只是停止下降,并给你对象的内部十六进制指针(errr,顺便说一下,这个指针并没有很多用处)。所以简而言之,python就是这个伟大的面向对象范式,但是你从盒子里拿出来的工具是为处理对象以外的东西而设计的。
perl Data::Dumper允许你控制你想要去的深度,还可以检测循环链接结构(这真的很重要)。这个过程在perl中更容易实现,因为对象除了它们的祝福之外没有任何特殊的魔力(一个普遍定义良好的过程)。