str()和repr()是做什么的? [英] What does !r do in str() and repr()?

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

问题描述

根据 Python 2.7.12文档
$ b


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

 >>>导入数学
>>> print'PI的值大约是{}。'format(math.pi)
PI的值大约是3.14159265359。
>>>打印'PI的值大约为{!r}。'format(math.pi)
PI的值大约为3.141592653589793。


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

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

那么转换价值在这里呢?使它不那么容易理解?

解决方案

为了在中设置一个字符串,必须先创建一些东西的表示。 转换价值基本上是在谈论如何构建字符串表示。在python中,有两个相当自然的选择来得到字符串表示的东西... str repr str 通常更友好一些, repr 通常更精确。也许官方文档是寻找差异:


object .__ repr __(self)


$ b

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

  • 这通常用于调试,所以




object .__ str __( self)



  • 通过str(object)和内置的函数格式()和print()来计算对象的非正式或很好的可打印字符串表示形式。返回值必须是一个字符串对象。

  • 这个方法不同于 object .__ repr __() in我们不希望 __ str __()返回一个有效的Python表达式:可以使用更方便或简洁的表示形式。


  • 由内置类型对象定义的默认实现调用 object .__ repr __()

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



可以很容易地看到字符串的区别( repr 为一个字符串将包括外部引号):

 >>> 'foo {}'。format('bar')
'foo bar'
>>> 'foo {!r}'。格式('bar')
foobar'



这两种方法之间的区别实际上取决于被格式化的对象。对于许多对象(例如那些不会覆盖 __ str __ 方法的对象),格式化输出将不会有任何区别。


According to the Python 2.7.12 documentation:

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

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?

解决方案

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)

  • 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)

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

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

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

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

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'"

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.

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

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