为什么 python F 字符串插值用引号引起来?

Why is this usage of python F-string interpolation wrapping with quotes?

有问题的代码:

a = 'test'

# 1)
print(f'{a}') # test

# 2)
print(f'{ {a} }') # {'test'}

# 3)
print(f'{{ {a} }}') # {test}

我的问题是,为什么案例二打印那些引号?

我没有在 documentation. The closest thing I found detailing this was in the PEP 中找到任何关于此功能的明确内容:

(F 字符串的语法)

f ' <text> { <expression> <optional !s, !r, or !a> <optional : format specifier> } <text> ... '

The expression is then formatted using the format protocol, using the format specifier as an argument. The resulting value is used when building the value of the f-string.

我假设 a 的值正在使用某种格式化程序进行格式化,由于数据类型是字符串,因此用引号将其括起来。然后将此结果返回到周围的 F 字符串格式化实例。

这个假设是否正确?有没有其他地方更清楚地记录这一点?

f'{ {a} }' 中,{a}(如语法所示)被解释为 Python 表达式。在Python中,{a}构造了一个元素(a)的set,集合的str化使用了它的repr元素,这是引号的来源。