Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';t获取python中的_repr__()方法的实现_Python_Python 3.x_Class_Methods_Tuples - Fatal编程技术网

Can';t获取python中的_repr__()方法的实现

Can';t获取python中的_repr__()方法的实现,python,python-3.x,class,methods,tuples,Python,Python 3.x,Class,Methods,Tuples,我当时正在读大卫·比兹利和布赖恩·琼斯写的关于python的名著《python食谱》。在“类和对象”一章中,他们编写了以下代码: class Point: def __init__(self,x,y): self.x = x self.y = y def __repr__(self): return 'Point({0.x!r}, {0.y!r})'.format(self) def __str__(self):

我当时正在读大卫·比兹利和布赖恩·琼斯写的关于python的名著《python食谱》。在“类和对象”一章中,他们编写了以下代码:

class Point:
    def __init__(self,x,y):
        self.x = x
        self.y = y
    def __repr__(self):
        return 'Point({0.x!r}, {0.y!r})'.format(self)
    def __str__(self):
        return '({0.x!s}, {0.y!s})'.format(self)

现在,只有我了解到,repr()方法用于向开发人员表示对象,str()用于向盲用户表示对象。但在这两种方法中,“返回”之后发生了什么?两种情况下肯定都有元组。但是元组有什么样的成员呢?我以前从未见过类似的情况。

您可能想在解释器中尝试一下,看看发生了什么:

>>> class Point:
    def __init__(self, x, y):
        """Initialize self."""
        self.x, self.y = x, y
    def __repr__(self):
        """Return repr(self)."""
        return f'{type(self).__name__}({self.x!r}, {self.y!r})'
    def __str__(self):
        """Return str(self)."""
        return f'({self.x!s}, {self.y!s})'

>>> Point(1, 2) # causes implicit call to __repr__
Point(1, 2)
>>> print(_) # causes implicit call to __str__
(1, 2)
>>> 

示例中的
\uuuu repr\uuuu
\uuuu str\uuuu
都返回格式化字符串,而不是元组。当显式或隐式地对对象调用
repr()
str()
时,将调用这些函数

例如,一个
print(点(1,2))
会打印
\uuuu str\uuu
的结果,因为它在内部调用对象上的
str()
,但是
print([点(1,2)]
会打印
\uu repr\uu
的结果


对于您自己的对象,这些方法可以返回您想要的任何内容,只要它们只是字符串。

下面是具体方法的分解。使用
str.format
方法,您可以在给定的字符串实例中填写形式为
{…}
的占位符。这些参数以以下方式处理位置参数:

>>> '{}:{}'.format(1, 2)
'1:2'
>>> '{0}:{1}'.format(1, 2)
'1:2'
>>> '{1}:{0}'.format(1, 2)
'2:1'
这意味着您可以在花括号内指定位置参数的数目。字符串可以多次引用位置参数:

>>> '{0}:{1}:{0}'.format(1, 2)
'1:2:1'
使用
.x
符号可以访问该参数的属性。例如:

>>> class Test:
...     x = 1
...     y = 2
... 
>>> '{0.x}:{0.y}'.format(Test)
'1:2'
>>> class Test:
...     def __str__(self):
...         return '__str__'
...     def __repr__(self):
...         return '__repr__'
... 
>>> '{0}'.format(Test())
'__str__'
>>> '{0!s}'.format(Test())
'__str__'
>>> '{0!r}'.format(Test())
'__repr__'
使用
!r
您可以强制使用该对象的
\uuu repr\uuu
,而不是
\uu str\uuu
。例如:

>>> class Test:
...     x = 1
...     y = 2
... 
>>> '{0.x}:{0.y}'.format(Test)
'1:2'
>>> class Test:
...     def __str__(self):
...         return '__str__'
...     def __repr__(self):
...         return '__repr__'
... 
>>> '{0}'.format(Test())
'__str__'
>>> '{0!s}'.format(Test())
'__str__'
>>> '{0!r}'.format(Test())
'__repr__'
因此,结合所有这些信息,我们得到以下信息:

'Point({0.x!r}, {0.y!r})'.format(self)
这里定义了一个带有两个占位符的格式字符串(
{0.x!r}
{0.y!r}
)。它们应该用第一个位置参数的
x
y
属性来填充(回想一下,
{0}
将被第一个参数替换,因此
{0.x}
请求该参数的
x
属性)。最后是
!r
请求该对象的
\uuuu repr\uuu
,而不是
\uuuu str\uuuu
(默认值)

同样的推理也适用于
\uuu str\uu
方法

顺便说一句,格式化语法还允许关键字参数,并且可以通过其名称引用它们:

>>> '{first}:{second}'.format(first=1, second=2)
'1:2'
>>> '{second}:{first}'.format(first=1, second=2)
'2:1'

仔细看,这两个函数都返回格式化字符串,而不是tupleSook true,但整个过程是如何工作的?它只是返回一个字符串。是的,但是那些{0.x!r}和所有的是什么?无法解读其含义。@SaswataMishra详细介绍了这一点。