在 python 解释器中键入一个对象的名称 - 它调用的是什么方法?
Type the name of an object in python interpreter - what method is it called?
当我输入对象的名称时,它调用了什么方法?
我一直认为它是在调用 repr
或 str
,但对于 PyPDF2 的 PageObject 来说,这并不成立。如您所见,__repr__
或 __str__
的输出与我们在交互式控制台中键入变量名称时得到的输出不同。
>>> reader = PdfFileReader(f)
>>> page = reader.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>
P.S。这个问题可能有答案,只是我没有正确输入我的查询。
UPDATE 我在 IPython(版本 5.5.0)中观察到这种行为。 运行 内置 REPL 输入变量名时得到的输出与 repr
输出匹配。
在标准 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;这意味着某些对象在显示时会得到特殊处理。字典就是这样的对象;因为你的 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
显示;这类似于使用 pprint.pprint
:
>>> 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'}
当我输入对象的名称时,它调用了什么方法?
我一直认为它是在调用 repr
或 str
,但对于 PyPDF2 的 PageObject 来说,这并不成立。如您所见,__repr__
或 __str__
的输出与我们在交互式控制台中键入变量名称时得到的输出不同。
>>> reader = PdfFileReader(f)
>>> page = reader.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>
P.S。这个问题可能有答案,只是我没有正确输入我的查询。
UPDATE 我在 IPython(版本 5.5.0)中观察到这种行为。 运行 内置 REPL 输入变量名时得到的输出与 repr
输出匹配。
在标准 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;这意味着某些对象在显示时会得到特殊处理。字典就是这样的对象;因为你的 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
显示;这类似于使用 pprint.pprint
:
>>> 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'}