Inspecting Python Objects
我正在看一个不再和我们一起工作的同事给我的代码。
我有一个名为rx的列表变量。
1 2 3 | >> type(rx) type 'list' |
当我查看Rx[0]的内部时,我会得到:
1 2 3 | >> rx[0] <Thing.thing.stuff.Rx object at 0x10e1e1c10> |
有人能翻译这意味着什么吗?更重要的是,我如何才能在Rx列表中看到这个对象内部的内容?
感谢您的帮助。
从帮助开始:
1 2 3 4 5 6 7 | # example python object class Employee: """Common base class for all employees.""" empCount = 0 help(Employee) |
输出:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | Help on class Employee in module __main__: class Employee(builtins.object) | Common base class for all employees. | | Data descriptors defined here: | | __dict__ | dictionary for instance variables (if defined) | | __weakref__ | list of weak references to the object (if defined) | | ---------------------------------------------------------------------- | Data and other attributes defined here: | | empCount = 0 |
如果信息不够,请检查检查模块。
inspect有许多可能有用的方法,如getmembers和getdoc:
1 2 3 4 5 6 7 8 | import inspect inspect.getdoc(Employee) # 'Common base class for all employees.' for name, data in inspect.getmembers(Employee): if name == '__builtins__': continue print('%s :' % name, repr(data)) |
输出:
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__ : <class 'type'> __delattr__ : <slot wrapper '__delattr__' of 'object' objects> __dict__ : mappingproxy({'__module__': '__main__', '__dict__': , '__weakref__': , 'empCount': 0, '__doc__': 'Common base class for all employees.'}) __dir__ : <method '__dir__' of 'object' objects> __doc__ : 'Common base class for all employees.' __eq__ : <slot wrapper '__eq__' of 'object' objects> __format__ : <method '__format__' of 'object' objects> __ge__ : <slot wrapper '__ge__' of 'object' objects> __getattribute__ : <slot wrapper '__getattribute__' of 'object' objects> __gt__ : <slot wrapper '__gt__' of 'object' objects> __hash__ : <slot wrapper '__hash__' of 'object' objects> __init__ : <slot wrapper '__init__' of 'object' objects> __le__ : <slot wrapper '__le__' of 'object' objects> __lt__ : <slot wrapper '__lt__' of 'object' objects> __module__ : '__main__' __ne__ : <slot wrapper '__ne__' of 'object' objects> __new__ : <built-in method __new__ of type object at 0x108a69d20> __reduce__ : <method '__reduce__' of 'object' objects> __reduce_ex__ : <method '__reduce_ex__' of 'object' objects> __repr__ : <slot wrapper '__repr__' of 'object' objects> __setattr__ : <slot wrapper '__setattr__' of 'object' objects> __sizeof__ : <method '__sizeof__' of 'object' objects> __str__ : <slot wrapper '__str__' of 'object' objects> __subclasshook__ : <built-in method __subclasshook__ of type object at 0x7faa994086e8> __weakref__ : empCount : 0 |