在python解释器中键入一个对象的名称它调用了什么方法?

2024-06-10 22:08:20 发布

您现在位置:Python中文网/ 问答频道 /正文

当我键入对象的名称时,它调用什么方法? 我一直认为它是在调用reprstr,但对于PyPDF2的PageObject,这一点并不成立。如您所见,__repr____str__的输出与在交互控制台中键入变量名称时得到的输出不同。你知道吗

>>> PDF = PdfFileReader(f)
>>> page = PDF.pages[0]
>>> page

'/Encoding': {'/Differences': [32,
      '/space',
      40,
      '/parenleft',
      '/parenright',
      46,
      '/period',
      '/slash',
      '/zero',
      '/one',
      '/two',
      '/three',
      '/four',
      '/five',
      '/six',
      56,
      '/eight',
      '/nine',
      69,
      ...

>>> page.__str__()

"{'/Annots': [], '/Contents': IndirectObject(12, 0), '/Group': {'/CS': '/DeviceRGB', '/S': '/Transparency', '/Type': '/Group'}, '/MediaBox': RectangleObject([0, 0, 460.8, 345.6]), '/Parent': IndirectObject(2, 0), '/Resources': IndirectObject(8, 0), '/Type': '/Page', '/ArtBox': RectangleObject([0, 0, 460.8, 345.6]), '/BleedBox': RectangleObject([0, 0, 460.8, 345.6]), '/CropBox': RectangleObject([0, 0, 460.8, 345.6]), '/TrimBox': RectangleObject([0, 0, 460.8, 345.6])}"
```

>>> page.__repr__()

<same-as-above>

另外,这个问题可能有答案,只是我没有正确地输入我的查询。你知道吗


更新我在IPython(5.5.0版)中观察到这种行为。使用内置REPL运行输入变量名时得到的输出与repr输出匹配。你知道吗


Tags: 对象方法名称键入pdftypepagegroup
1条回答
网友
1楼 · 发布于 2024-06-10 22:08:20

在标准Python REPL中使用变量名(如x)相当于print(repr(x))。你可以通过自己实现__repr____str__来说服自己(而且这不仅仅是print(x)):

>>> class Test:
...   def __repr__(self):
...     return 'using repr\nmagic, isn\'t it?'
...   def __str__(self):
...     return 'using str'
... 
>>> Test()
using repr
magic, isn't it?
>>> repr(Test())
"using repr\nmagic, isn't it?"
>>> print(Test())
using str
>>> print(repr(Test()))
using repr
magic, isn't it?

但是您使用的是IPython,它的特性是rich outputs;这意味着某些对象在显示时会得到特殊处理。dict就是这样的对象;由于您的page是一种特殊的dict

Help on PageObject in module PyPDF2.pdf object:

class PageObject(PyPDF2.generic.DictionaryObject)
 |  PageObject(pdf=None, indirectRef=None)
 |  
 |  This class represents a single page within a PDF file.  Typically this
 |  object will be created by accessing the
 |  :meth:`getPage()<PyPDF2.PdfFileReader.getPage>` method of the
 |  :class:`PdfFileReader<PyPDF2.PdfFileReader>` class, but it is
 |  also possible to create an empty page with the
 |  :meth:`createBlankPage()<PageObject.createBlankPage>` static method.
 |  
 |  :param pdf: PDF file the page belongs to.
 |  :param indirectRef: Stores the original indirect reference to
 |      this object in its source PDF
 |  
 |  Method resolution order:
 |      PageObject
 |      PyPDF2.generic.DictionaryObject
 |      builtins.dict
 |      PyPDF2.generic.PdfObject
 |      builtins.object
[…snip…]

然后得到特殊的dict显示;这类似于使用^{}

>>> import pprint
>>> from PyPDF2 import PdfFileReader
>>> PDF = PdfFileReader('…')
>>> page = PDF.pages[0]
>>> pprint.pprint(page)
{'/Contents': IndirectObject(2, 0),
 '/Group': {'/CS': '/DeviceRGB',
            '/I': <PyPDF2.generic.BooleanObject object at 0x7faa67639310>,
            '/S': '/Transparency'},
 '/MediaBox': [0, 0, 842, 595],
 '/Parent': IndirectObject(6, 0),
 '/Resources': IndirectObject(23, 0),
 '/Rotate': 0,
 '/Type': '/Page'}

相关问题 更多 >