嵌套/转义 f 字符串“=”(等号)表达式

Nesting / escaping a f-string "=" (equal sign) expression

是否可以在“=”表达式中转义 python f 字符串? (Python 3.8 中的新功能)
例如我有以下表达式:

def double(x):
    return x * 2
some_int = 2

print(f'{double(some_int) = }') 

打印:

double(some_int) = 4

是否可以以某种方式转义 some_int 参数,以便打印:

double(2) = 4

或者我是否必须输入两次表达式并连接文字表达式并以老式方式生成结果?

您的代码没有 运行,但我认为您打算这样做:

some_int = 2
print(f'double({some_int}) = {some_int*2}')

打印:

double(2) = 4

What's New In Python 3.8 文档中所述:

Added an = specifier to f-strings. An f-string such as f'{expr=}' will expand to the text of the expression, an equal sign, then the representation of the evaluated expression.

所以不,你不能那样做(一次)因为 = 的左边会变成一个字符串。使用传统的 f 字符串插值:

print(f'double({some_int}) = {double(some_int)}')

输出:

double(2) = 4