这里我要找的是PHP的print_r函数。

这样我就可以通过查看有问题的对象的状态来调试脚本。


当前回答

我喜欢使用python对象内置类型、键或值。

对于属性,不管它们是方法还是变量:

o.keys()

对于这些属性的值:

o.values()

其他回答

你可以使用"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

要打印对象的当前状态,你可以:

>>> obj # in an interpreter

or

print repr(obj) # in a script

or

print obj

为你的类定义__str__或__repr__方法。来自Python文档:

__repr__(self)由repr()内置函数调用,由字符串调用 转换(反引号)到 计算“官方”字符串 对象的表示。如果有的话 可能,这看起来像a 有效的Python表达式 方法重新创建对象 相同的值(给定一个合适的 环境)。如果不可能, 形式为“<…一些有用的 描述……应该返回>。 返回值必须为字符串 对象。如果一个类定义了repr() 但不是__str__(),则__repr__()是 也用于“非正式”字符串 实例的表示 班级是必需的。这是典型的 用于调试,所以很重要 这个表示是 信息丰富且明确。 __str__(self)由str()内置函数调用,由print函数调用 语句来计算“非正式的” 对象的字符串表示形式。 这与__repr__()不同 它不必是一个有效的Python 表达:比较方便还是 可以使用简洁的表示 代替。返回值必须为a 字符串对象。

一个有魔力的元编程示例转储对象:

$ 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>

虽然有点过时,但还能用。

在大多数情况下,使用__dict__或dir()将获得你想要的信息。如果您碰巧需要更多的细节,标准库包括inspect模块,它允许您获得一些令人印象深刻的细节。一些真正宝贵的信息包括:

函数和方法参数的名称 类层次结构 实现一个函数/类对象的源代码 帧对象的局部变量

如果你只是在寻找“我的对象有哪些属性值?”,那么dir()和__dict__可能就足够了。如果您真的想深入研究任意对象的当前状态(请记住,在python中几乎所有东西都是对象),那么inspect值得考虑。

已经提到了Dir,但它只提供属性的名称。如果你也想要它们的值,试试__dict__。

class O:
   def __init__ (self):
      self.value = 3

o = O()

输出如下:

>>> o.__dict__

{'value': 3}