这里我要找的是PHP的print_r函数。
这样我就可以通过查看有问题的对象的状态来调试脚本。
这里我要找的是PHP的print_r函数。
这样我就可以通过查看有问题的对象的状态来调试脚本。
当前回答
一个有魔力的元编程示例转储对象:
$ cat dump.py
#!/usr/bin/python
import sys
if len(sys.argv) > 2:
module, metaklass = sys.argv[1:3]
m = __import__(module, globals(), locals(), [metaklass])
__metaclass__ = getattr(m, metaklass)
class Data:
def __init__(self):
self.num = 38
self.lst = ['a','b','c']
self.str = 'spam'
dumps = lambda self: repr(self)
__str__ = lambda self: self.dumps()
data = Data()
print data
无参数:
$ python dump.py
<__main__.Data instance at 0x00A052D8>
与Gnosis Utils:
$ python dump.py gnosis.magic MetaXMLPickler
<?xml version="1.0"?>
<!DOCTYPE PyObject SYSTEM "PyObjects.dtd">
<PyObject module="__main__" class="Data" id="11038416">
<attr name="lst" type="list" id="11196136" >
<item type="string" value="a" />
<item type="string" value="b" />
<item type="string" value="c" />
</attr>
<attr name="num" type="numeric" value="38" />
<attr name="str" type="string" value="spam" />
</PyObject>
虽然有点过时,但还能用。
其他回答
从答案中,可以稍微修改一下,只得到一个对象的“Attributes”,如下所示:
def getAttributes(obj):
from pprint import pprint
from inspect import getmembers
from types import FunctionType
def attributes(obj):
disallowed_names = {
name for name, value in getmembers(type(obj))
if isinstance(value, FunctionType)}
return {
name for name in dir(obj)
if name[0] != '_' and name not in disallowed_names and hasattr(obj, name)}
pprint(attributes(obj))
在临时添加此函数时非常有用,并且可以在不修改现有源代码的情况下删除此函数
你需要vars()和pprint()的混合:
from pprint import pprint
pprint(vars(your_object))
你可以使用"dir()"函数来做到这一点。
>>> import sys
>>> dir(sys)
['__displayhook__', '__doc__', '__excepthook__', '__name__', '__stderr__', '__stdin__', '__stdo
t__', '_current_frames', '_getframe', 'api_version', 'argv', 'builtin_module_names', 'byteorder
, 'call_tracing', 'callstats', 'copyright', 'displayhook', 'dllhandle', 'exc_clear', 'exc_info'
'exc_type', 'excepthook', 'exec_prefix', 'executable', 'exit', 'getcheckinterval', 'getdefault
ncoding', 'getfilesystemencoding', 'getrecursionlimit', 'getrefcount', 'getwindowsversion', 'he
version', 'maxint', 'maxunicode', 'meta_path', 'modules', 'path', 'path_hooks', 'path_importer_
ache', 'platform', 'prefix', 'ps1', 'ps2', 'setcheckinterval', 'setprofile', 'setrecursionlimit
, 'settrace', 'stderr', 'stdin', 'stdout', 'subversion', 'version', 'version_info', 'warnoption
', 'winver']
>>>
另一个有用的功能是帮助。
>>> help(sys)
Help on built-in module sys:
NAME
sys
FILE
(built-in)
MODULE DOCS
http://www.python.org/doc/current/lib/module-sys.html
DESCRIPTION
This module provides access to some objects used or maintained by the
interpreter and to functions that interact strongly with the interpreter.
Dynamic objects:
argv -- command line arguments; argv[0] is the script pathname if known
我喜欢使用python对象内置类型、键或值。
对于属性,不管它们是方法还是变量:
o.keys()
对于这些属性的值:
o.values()
也许值得一查——
Python中是否存在与Perl的Data:: dump相同的程序?
我的建议是——
https://gist.github.com/1071857
注意,perl有一个名为Data::Dumper的模块,它将对象数据转换回perl源代码(注意:它不将代码转换回源代码,而且几乎总是你不想在输出中使用对象方法函数)。这可以用于持久性,但常见的目的是用于调试。
标准的python pprint有很多事情无法实现,特别是当它看到一个对象的实例时,它只是停止下降,并给你对象的内部十六进制指针(errr,顺便说一下,这个指针并没有很多用处)。所以简而言之,python就是这个伟大的面向对象范式,但是你从盒子里拿出来的工具是为处理对象以外的东西而设计的。
perl Data::Dumper允许你控制你想要去的深度,还可以检测循环链接结构(这真的很重要)。这个过程在perl中更容易实现,因为对象除了它们的祝福之外没有任何特殊的魔力(一个普遍定义良好的过程)。