Looking for better data inspector than `dir`
Possible Duplicate:
Is there a function in Python to print all the current properties and values of an object?
号
在交互式Python会话中,我经常使用
Where can I find an"off-the-shelf" data-inspection utility that is more informative, and better formatted,
dir ?
号
例如,对于
谢谢!
尝试inspect模块,它可以为您提供各种各样的东西,包括函数的原始源代码、堆栈帧等:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | >>> class Foo: ... def __init__(self, a, b, c): ... self.a = a ... self.b = b ... self.c = c ... def foobar(self): ... return 100 ... >>> f = Foo(50, 'abc', 2.5) >>> import inspect >>> inspect.getmembers(f) [('__doc__', None), ('__init__', <bound method Foo.__init__ of <__main__.Foo instance at 0x7f0d76c440e0>>), ('__module__', '__main__'), ('a', 50), ('b', 'abc'), ('c', 2.5), ('foobar', <bound method Foo.foobar of <__main__.Foo instance at 0x7f0d76c440e0>>)] >>> >>> def add5(x): ... """adds 5 to arg""" ... return 5 + x ... >>> inspect.getdoc(add5) 'adds 5 to arg' >>> # What arguments does add5 take? >>> inspect.getargspec(add5) ArgSpec(args=['x'], varargs=None, keywords=None, defaults=None) >>> # Format that nicely... >>> inspect.formatargspec(inspect.getargspec(add5)) '((x,), None, None, None)' >>> |
如果"对象的结构"是指对象的实例属性,请注意EDOCX1(something)调用某个对象。
因此,我相信一个对象的实例属性是由它的
但有些对象没有实例属性,例如整数。
因此,我认为,如果你测试一个对象的
由于您要求格式化输出,所以我用cpickle给出了这个示例(模块是一个对象,与Python中的所有内容一样)。
1 2 3 4 | import cPickle for name,obj in cPickle.__dict__.iteritems() : print '%-25s --> %r' % (name, obj) |
结果
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | load --> <built-in function load> PicklingError --> <class 'cPickle.PicklingError'> __version__ --> '1.71' UnpickleableError --> <class 'cPickle.UnpickleableError'> dump --> <built-in function dump> __builtins__ --> <module '__builtin__' (built-in)> Unpickler --> <built-in function Unpickler> compatible_formats --> ['1.0', '1.1', '1.2', '1.3', '2.0'] BadPickleGet --> <class 'cPickle.BadPickleGet'> __package__ --> None dumps --> <built-in function dumps> UnpicklingError --> <class 'cPickle.UnpicklingError'> PickleError --> <class 'cPickle.PickleError'> HIGHEST_PROTOCOL --> 2 __name__ --> 'cPickle' loads --> <built-in function loads> Pickler --> <built-in function Pickler> __doc__ --> 'C implementation and optimization of the Python pickle module.' format_version --> '2.0' |
。
.
还有一个函数help(),它提供了您正在搜索的文档。
查看pprint
1 2 | from pprint import pprint pprint(object_you_want_to_see) |
号