!r 在 str() 和 repr() 中做了什么? [英] What does !r do in str() and repr()?

查看:19
本文介绍了!r 在 str() 和 repr() 中做了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

根据 Python 2.7.12 文档:

!s (apply str()) 和 !r (apply repr()) 可以用来转换格式化前的值.

!s (apply str()) and !r (apply repr()) can be used to convert the value before it is formatted.

>>> import math
>>> print 'The value of PI is approximately {}.'.format(math.pi)
The value of PI is approximately 3.14159265359.
>>> print 'The value of PI is approximately {!r}.'.format(math.pi)
The value of PI is approximately 3.141592653589793.

有趣的是,转换后的值是repr()的输出,而不是str().

Interestingly, the converted value is the output of repr(), rather than str().

>>> str(math.pi)
'3.14159265359'
>>> repr(math.pi)
'3.141592653589793'

那么这里的转换值"是什么意思呢?让它不那么可读?

So what does "convert the value" mean here? Making it less human-readable?

推荐答案

为了在字符串中格式化某些东西,必须首先创建该东西的字符串表示.转换值"基本上是在谈论如何构造字符串表示.在 python 中,有两种相当自然的选择来获取某物的字符串表示形式... strrepr.str 通常更人性化一点,repr 通常更精确.也许 官方文档 是寻找区别:

In order to format something in a string, a string representation of that something must first be created. "convert the value" is basically talking about how the string representation is to be constructed. In python, there are two fairly natural choices to get a string representation of something ... str and repr. str is generally a little more human friendly, repr is generally more precise. Perhaps the official documentation is the best place to go looking for the difference:

object.__repr__(self)

  • repr() 内置函数调用以计算对象的官方"字符串表示.如果可能的话,这应该看起来像一个有效的 Python 表达式,可用于重新创建具有相同值的对象(给定适当的环境).如果这不可能,则应返回 <...一些有用的描述...> 形式的字符串.返回值必须是字符串对象.如果一个类定义了 __repr__() 而不是 __str__(),那么当实例的非正式"字符串表示时也使用 __repr__()该类是必需的.

  • Called by the repr() built-in function to compute the "official" string representation of an object. If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment). If this is not possible, a string of the form <...some useful description...> should be returned. The return value must be a string object. If a class defines __repr__() but not __str__(), then __repr__() is also used when an "informal" string representation of instances of that class is required.

这通常用于调试,因此表示信息丰富且明确是很重要的.

This is typically used for debugging, so it is important that the representation is information-rich and unambiguous.

object.__str__(self)

  • 由 str(object) 和内置函数 format() 和 print() 调用,以计算对象的非正式"或可良好打印的字符串表示.返回值必须是字符串对象.

  • Called by str(object) and the built-in functions format() and print() to compute the "informal" or nicely printable string representation of an object. The return value must be a string object.

此方法与 object.__repr__() 的不同之处在于不期望 __str__() 返回有效的 Python 表达式:更方便或更简洁可以使用表示.

This method differs from object.__repr__() in that there is no expectation that __str__() return a valid Python expression: a more convenient or concise representation can be used.

内置类型对象定义的默认实现调用object.__repr__().

The default implementation defined by the built-in type object calls object.__repr__().

str.format 中,!s 选择使用 str 来格式化对象,而 !r 选择repr 格式化值.

In str.format, !s chooses to use str to format the object whereas !r chooses repr to format the value.

使用字符串可以很容易地看出差异(因为字符串的 repr 将包含外引号).:

The difference can easily be seen with strings (as repr for a string will include outer quotes).:

>>> 'foo {}'.format('bar')
'foo bar'
>>> 'foo {!r}'.format('bar')
"foo 'bar'"

这两种方法之间的区别在很大程度上取决于被格式化的对象.对于许多对象(例如那些不覆盖 __str__ 方法的对象),格式化输出不会有任何差异.

What the difference between these two methods really depends critically on the objects being formatted. For many objects (e.g. those that don't override the __str__ method), there will be no difference in the formatted output.

这篇关于!r 在 str() 和 repr() 中做了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆