【问题标题】:Inspecting Python Objects检查 Python 对象
【发布时间】:2017-01-08 16:21:24
【问题描述】:

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

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

>> type(rx)

type 'list'

当我查看 rx[0] 内部时,我得到了这个:

>> rx[0]

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

谁能翻译一下这是什么意思?而且,更重要的是,我如何才能在 rx 列表中查看此对象内部的内容?

感谢任何帮助。

【问题讨论】:

  • 是repr输出,打印出来会怎样?
  • 这是link to repr
  • 首先通过Thing.thing.stuff.Rx(或任何真实的东西)定位Rx对象的源
  • 如果你以前的人勤奋地为Rx 对象编写文档字符串,请尝试help(rx[0]) 并祈祷有用的文档。或者试试help(Thing.thing.stuff.Rx)
  • 您的问题似乎与列表中的对象没有任何基本关系。真的是,“我有一些我不知道的类型的对象,我应该如何使用它?”。

标签: python object indexing


【解决方案1】:

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 有很多可能有用的方法,例如 getmembersgetdoc

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

【讨论】:

  • 如何过滤掉 slot wrappers?
猜你喜欢
  • 2011-11-23
  • 1970-01-01
  • 1970-01-01
  • 2011-02-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-06-18
  • 2022-01-21
相关资源
最近更新 更多