众所周知,在Python f-strings中使用{}可以执行代码片段并以string
格式给出结果(一些教程为here).然而,表达式末尾的"="是什么意思?
log_file = open("log_aug_19.txt", "w")
console_error = '...stuff...' # the real code generates it with regex
log_file.write(f'{console_error=}')
众所周知,在Python f-strings中使用{}可以执行代码片段并以string
格式给出结果(一些教程为here).然而,表达式末尾的"="是什么意思?
log_file = open("log_aug_19.txt", "w")
console_error = '...stuff...' # the real code generates it with regex
log_file.write(f'{console_error=}')
这实际上是brand-new feature as of Python 3.8.
为f字符串添加了
=
说明符.f字串,如f'{expr=}'
本质上,它促进了打印调试的频繁使用,因此,尽管我们通常必须编写:
f"some_var={some_var}"
我们现在可以写:
f"{some_var=}"
因此,作为一个演示,使用一个全新的Python 3.8.0 REPL:
>>> print(f"{foo=}")
foo=42
>>>