在 Python 格式(f-string)字符串中,!r 是什么意思? [英] In Python format (f-string) strings, what does !r mean?

查看:592
本文介绍了在 Python 格式(f-string)字符串中,!r 是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解了 python 3.6 中新的 f 字符串的作用,但是下面的代码片段中的结尾 !r 呢?

I get what the new f strings in python 3.6 do, but what about the ending !r as found in the code snip below.

def __repr__(self):
    return (f'Pizza({self.radius!r}, 'f'{self.ingredients!r})')

推荐答案

它只是调用所提供值的 repr.

It just calls the repr of the value supplied.

f-strings 通常不需要它的用法,因为使用它们你可以只做 repr(self.radius) ,它的意图可以说更清晰.

It's usage is generally not really needed with f-strings since with them you can just do repr(self.radius) which is arguably more clear in its intent.

!r (repr)、!s (str) 和 !a (ascii) 只是为了简化与 str.format 替代方案的兼容性,您不需要将它们与 f-strings 一起使用.

!r (repr), !s (str) and !a (ascii) were kept around just to ease compatibility with the str.format alternative, you don't need to use them with f-strings.

这篇关于在 Python 格式(f-string)字符串中,!r 是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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