我如何使用内省来获取 Python 来告诉我更多关于 PIL ImagingCore 对象的信息?

How can I use introspection to get Python to tell me more about PIL ImagingCore objects?

我在 PIL 中做的事情是返回一个我不认识的 class 对象。它可能是 C 数据结构或其他东西的相当薄的包装器。我怎样才能让 Python 告诉我在哪里可以找到更多信息?

以下是使用内省来了解更多信息的一些失败尝试:

>>> import os, PIL
>>> obj = PIL.Image.open(os.path.expanduser("~/Desktop/foo.png")).getdata()
>>> type(obj)
<type 'ImagingCore'>
>>> ImagingCore
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'ImagingCore' is not defined
>>> PIL.ImagingCore
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'ImagingCore'
>>> obj.__class__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: __class__
>>> obj.__module__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: __module__
>>> import inspect
>>> inspect.getsource(obj)
...
TypeError: <ImagingCore object at 0x105e64b50> is not a module, class, method, function, traceback, frame, or code object
>>> inspect.getsource(type(obj))
...TypeError: <module '__builtin__' (built-in)> is a built-in class
>>>

PIL 的核心功能在模块 _imaging 中实现,如您所料,是用 C 语言编写的——请参阅顶级源代码中的 _imaging.c(3281 行...:-)目录,Imaging-1.1.7。该代码不关注内省——相反,它 100% 关注性能。我相信它甚至不会费心去暴露函数以外的任何东西(它确实实现了多种类型,包括 ImagingCore,但甚至没有将这些类型的名称暴露给 Python——仅在内部生成和使用它们)。

所以 Python 不会告诉您在哪里可以找到更多信息,因为图书馆反过来也不会告诉您 Python:-)。正如 getdatahttp://effbot.org/imagingbook/image.htm 的文档所说:

Note that the sequence object returned by this method is an internal PIL data type, which only supports certain sequence operations, including iteration and basic sequence access. To convert it to an ordinary sequence (e.g. for printing), use list(im.getdata())

...和 ​​"that's all she wrote" -- 除了序列操作的一小部分,什么都没有暴露。