代码之家  ›  专栏  ›  技术社区  ›  kuzzooroo

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

  •  0
  • kuzzooroo  · 技术社区  · 10 年前

    我在PIL中做的事情是给我一个我不认识的类的对象。它可能是一个围绕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
    >>>
    
    1 回复  |  直到 10 年前
        1
  •  1
  •   Alex Martelli    10 年前

    PIL的核心功能在模块中实现 _imaging ,如您所猜,是用C语言编写的--请参见 _imaging.c (3281行…:-)在顶级源目录Imaging--1.1.7中。该代码不关注内省——相反,它100%关注性能。我相信它甚至不需要公开任何东西,只需要函数(它确实实现了几个类型,包括ImagingCore,但甚至不向Python公开这些类型的名称——只在内部生成和使用它们)。

    因此Python不会告诉您在哪里查找更多信息,因为库反过来也不会告诉Python:-)。作为的文档 getdata http://effbot.org/imagingbook/image.htm 说:

    注意,此方法返回的序列对象是一个内部 PIL数据类型仅支持某些序列操作, 包括迭代和基本序列访问。将其转换为 普通序列(例如用于打印),使用 list(im.getdata())

    ……“这就是她写的全部内容”——只有一小部分序列操作被暴露出来。