%r 是什么意思?

发布于 2024-08-23 06:37:35 字数 150 浏览 12 评论 0原文

下面语句中的%r是什么意思?

print '%r' % (1)

我想我听说过 %s%d%f 但从未听说过这个。

What's the meaning of %r in the following statement?

print '%r' % (1)

I think I've heard of %s, %d, and %f but never heard of this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

三生殊途 2024-08-30 06:37:35

背景:

在Python中,有两个内置函数用于将对象转换为字符串: < code>strreprstr 应该是一个友好的、人类可读的字符串。 repr 应该包含有关对象内容的详细信息(有时,它们会返回相同的内容,例如整数)。按照惯例,如果有一个 Python 表达式将求值为另一个 == 对象,则 repr 将返回这样的表达式,例如,

>>> print repr('hi')
'hi'  # notice the quotes here as opposed to...
>>> print str('hi')
hi

如果返回表达式对于对象没有意义,则 repr< /code> 应该返回一个由 < 包围的字符串。和>符号例如

要回答您原来的问题:

%s <-> str
%r <-> repr

另外:

您可以通过实现 来控制您自己的类的实例转换为字符串的方式__str____repr__ 方法。

class Foo:

  def __init__(self, foo):
    self.foo = foo

  def __eq__(self, other):
    """Implements ==."""
    return self.foo == other.foo

  def __repr__(self):
    # if you eval the return value of this function,
    # you'll get another Foo instance that's == to self
    return "Foo(%r)" % self.foo

Background:

In Python, there are two builtin functions for turning an object into a string: str vs. repr. str is supposed to be a friendly, human readable string. repr is supposed to include detailed information about an object's contents (sometimes, they'll return the same thing, such as for integers). By convention, if there's a Python expression that will eval to another object that's ==, repr will return such an expression e.g.

>>> print repr('hi')
'hi'  # notice the quotes here as opposed to...
>>> print str('hi')
hi

If returning an expression doesn't make sense for an object, repr should return a string that's surrounded by < and > symbols e.g. <blah>.

To answer your original question:

%s <-> str
%r <-> repr

In addition:

You can control the way an instance of your own classes convert to strings by implementing __str__ and __repr__ methods.

class Foo:

  def __init__(self, foo):
    self.foo = foo

  def __eq__(self, other):
    """Implements ==."""
    return self.foo == other.foo

  def __repr__(self):
    # if you eval the return value of this function,
    # you'll get another Foo instance that's == to self
    return "Foo(%r)" % self.foo
屋檐 2024-08-30 06:37:35

它在对象上调用 repr() 并插入结果字符串。

It calls repr() on the object and inserts the resulting string.

感性不性感 2024-08-30 06:37:35

添加到上面给出的回复中,'%r' 在您拥有具有异构数据类型的列表的情况下非常有用。
假设我们有一个 list = [1, 'apple' , 2 , 'r','banana']
显然,在这种情况下使用 '%d''%s' 会导致错误。相反,我们可以使用 '%r' 来打印所有这些值。

Adding to the replies given above, '%r' can be useful in a scenario where you have a list with heterogeneous data type.
Let's say, we have a list = [1, 'apple' , 2 , 'r','banana']
Obviously in this case using '%d' or '%s' would cause an error. Instead, we can use '%r' to print all these values.

凤舞天涯 2024-08-30 06:37:35

它使用 repr() 将替换内容打印为字符串。

It prints the replacement as a string with repr().

千仐 2024-08-30 06:37:35

%r%s 之间的区别在于,%r 调用的是 repr() 方法,而 %s 调用 str() 方法。这两个都是 Python 内置函数。

repr() 方法返回给定对象的可打印表示形式。
str() 方法返回给定对象的“非正式”或可良好打印的表示形式。

用简单的语言来说,str() 方法的作用是以最终用户希望看到的方式打印结果:

name = "Adam"
str(name)
Out[1]: 'Adam'

repr() 方法将打印或显示一个物体实际上是什么样子的:

name = "Adam"
repr(name)
Out[1]: "'Adam'"

The difference between %r and %s is, %r calls the repr() method and %s calls the str() method. Both of these are built-in Python functions.

The repr() method returns a printable representation of the given object.
The str() method returns the "informal" or nicely printable representation of a given object.

In simple language, what the str() method does is print the result in a way which the end user would like to see:

name = "Adam"
str(name)
Out[1]: 'Adam'

The repr() method would print or show what an object actually looks like:

name = "Adam"
repr(name)
Out[1]: "'Adam'"
蛮可爱 2024-08-30 06:37:35
%s <=> str
%r <=> repr

%r 调用 repr()到对象上,并插入由 __repr__ 返回的结果字符串。

__repr__ 返回的字符串应该是明确的,并且如果可能的话,与重新创建所表示的对象所需的源代码相匹配。

一个简单的例子:

class Foo:

    def __init__(self, foo):
        self.foo = foo


    def __repr__(self):
        return 'Foo(%r)' % self.foo

    def __str__(self):
        return self.foo


test = Foo('Text')

所以,

in[1]: test
Out[1]: Foo('Text')


in[2]: str(test)
Out[2]: 'Text'
%s <=> str
%r <=> repr

%r calls repr() on the object, and inserts the resulting string returned by __repr__.

The string returned by __repr__ should be unambiguous and, if possible, match the source code necessary to recreate the object being represented.

A quick example:

class Foo:

    def __init__(self, foo):
        self.foo = foo


    def __repr__(self):
        return 'Foo(%r)' % self.foo

    def __str__(self):
        return self.foo


test = Foo('Text')

So,

in[1]: test
Out[1]: Foo('Text')


in[2]: str(test)
Out[2]: 'Text'
仅此而已 2024-08-30 06:37:35

%s 调用所选对象的 __str()__ 方法并用返回值替换自身,

%r 调用 __repr所选对象的 ()__ 方法并用返回值替换其自身。

%s calls the __str()__ method of the selected object and replaces itself with the return value,

%r calls the __repr()__ method of the selected object and replaces itself with the return value.

醉态萌生 2024-08-30 06:37:35

请参阅文档中的字符串格式化操作。请注意,如果您习惯了它们在其他语言(例如 C)中的工作方式,%s 和 %d 等的工作方式可能与您期望的方式不同。

特别是,%s 也适用于整数和浮点数,除非您有特殊的格式要求其中 %d 或 %f 会给你更多的控制权。

See String Formatting Operations in the docs. Notice that %s and %d etc, might work differently to how you expect if you are used to the way they work in another language such as C.

In particular, %s also works well for ints and floats unless you have special formatting requirements where %d or %f will give you more control.

恰似旧人归 2024-08-30 06:37:35

我读过《Learning Python the Hard Way》,作者说

%r 最适合调试,其他格式用于向用户显示变量

I read in "Learning Python the Hard Way", the author said that

%r is the best for debugging, other formats are for displaying variables to users

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文