简体   繁体   English

r 在 str() 和 repr() 中有什么作用?

[英]What does !r do in str() and repr()?

According to the Python 2.7.12 documentation :根据Python 2.7.12 文档

!s (apply str() ) and !r (apply repr() ) can be used to convert the value before it is formatted. !s (apply str() ) 和!r (apply repr() ) 可用于在格式化之前转换值。

 >>> 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.

Interestingly, the converted value is the output of repr() , rather than str() .有趣的是,转换后的值是repr()的 output ,而不是str()

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

So what does "convert the value" mean here?那么这里的“转换价值”是什么意思呢? Making it less human-readable?使其不那么可读?

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 . 在python中,有两个相当自然的选择来获取某些东西的字符串表示... strrepr str is generally a little more human friendly, repr is generally more precise. str通常更人性化, repr通常更精确。 Perhaps the official documentation is the best place to go looking for the difference: 也许官方文档是寻找差异的最佳位置:

object.__repr__(self)

  • Called by the repr() built-in function to compute the “official” string representation of an object. repr()内置函数调用以计算对象的“官方”字符串表示。 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). 如果可能的话,这应该看起来像一个有效的Python表达式,可用于重新创建具有相同值的对象(给定适当的环境)。 If this is not possible, a string of the form <...some useful description...> should be returned. 如果这不可能,则应返回<...some useful description...>形式的字符串。 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. 如果一个类定义了__repr__()而不是__str__() ,那么当需要该类的实例的“非正式”字符串表示时,也会使用__repr__()

  • This is typically used for debugging, so it is important that the representation is information-rich and unambiguous. 这通常用于调试,因此表示信息丰富且明确是很重要的。

object.__str__(self)

  • Called by str(object) and the built-in functions format() and print() to compute the “informal” or nicely printable string representation of an object. 由str(对象)和内置函数format()和print()调用,以计算对象的“非正式”或可良好打印的字符串表示形式。 The return value must be a string object. 返回值必须是字符串对象。

  • 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__()不同之处在于,没有期望__str__()返回有效的Python表达式:可以使用更方便或简洁的表示。

  • The default implementation defined by the built-in type object calls object.__repr__() . 内置类型对象定义的默认实现调用object.__repr__()

In str.format , !s chooses to use str to format the object whereas !r chooses repr to format the value. str.format!s选择使用str来格式化对象,而!r选择repr来格式化值。

The difference can easily be seen with strings (as repr for a string will include outer quotes).: 使用字符串很容易看出差异(因为字符串的repr将包括外部引号):

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

What the difference between these two methods really depends critically on the objects being formatted. 这两种方法之间的区别究竟真正取决于被格式化的对象。 For many objects (eg those that don't override the __str__ method), there will be no difference in the formatted output. 对于许多对象(例如那些不覆盖__str__方法的对象),格式化输出没有区别。

I called the str(object) and the function -format() and print() to compute the as someone called it “informal”.我调用了 str(object) 和函数 -format() 和 print() 来计算有人称之为“非正式”的值。

To perfectly print string representation of an object.完美打印对象的字符串表示。 The return value must be a string object as well.返回值也必须是字符串对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM