检查Python对象

时间:2016-08-31 23:12:41

标签: python object indexing

我正在查看一位不再与我们合作的同事给我的代码。

我有一个名为rx的列表变量。

>> type(rx)

type 'list'

当我进入rx [0]时,我得到了这个:

>> rx[0]

<Thing.thing.stuff.Rx object at 0x10e1e1c10>

任何人都可以翻译这意味着什么吗?而且,更重要的是,如何在rx列表中查看此对象内的内容?

感谢任何帮助。

1 个答案:

答案 0 :(得分:2)

help开始:help(rx[0])

# example python object
class Employee:
    """Common base class for all employees."""
    empCount = 0


help(Employee)

输出:

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 module

Inspect有很多可能有用的方法,例如 getmembers getdoc

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

输出:

__class__ : <class 'type'>
__delattr__ : <slot wrapper '__delattr__' of 'object' objects>
__dict__ : mappingproxy({'__module__': '__main__', '__dict__': <attribute '__dict__' of 'Employee' objects>, '__weakref__': <attribute '__weakref__' of 'Employee' objects>, '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__ : <attribute '__weakref__' of 'Employee' objects>
empCount : 0