python - !r 叫什么?

标签 python syntax

我第一次看到这个。我想知道是什么!r在调用的代码的最后一行中,以便我可以搜索它。我在以下位置找到了这段代码:https://adamj.eu/tech/2020/08/10/a-guide-to-python-lambda-functions/

class Puppy:
    def __init__(self, name, cuteness):
        self.name = name
        self.cuteness = cuteness

    def __repr__(self):
        return f"Puppy({self.name!r}, {self.cuteness!r})"

最佳答案

这是一个格式字符串 conversion flag告诉格式化程序调用 repr在格式化字符串之前在对象上。

Three conversion flags are currently supported: '!s' which calls str() on the value, '!r' which calls repr() and '!a' which calls ascii().

关于python - !r 叫什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63475529/

相关文章:

python - Python 中是否有用于生成随机字符串的模块?

python - 计算字符串中的字母数(不是字符,只有字母)

python - PIL 在终端中工作,但不能通过 IDLE

Python 合并两个 csv 文件 python

python - 正则表达式处理双反斜杠

python - Eclipse 中的 HTML/CSS/JS 语法高亮显示

javascript - …不是 javascript {自定义代码}中的函数

C中变量名后的冒号

c++ - c++ 中的 friend 是相互的吗?

android - SQLITE CREATE VIEW JOIN 的语法问题