Python 3.8 f 字符串=
syntax
它已经到了!
#!/usr/bin/env python3
foo = 1
bar = 2
print(f"{foo=} {bar=}")
output:
foo=1 bar=2
添加到提交中https://github.com/python/cpython/commit/9a4135e939bc223f592045a38e0f927ba170da32“使用‘=’添加 f 字符串调试。”哪些文件:
f-strings now support = for quick and easy debugging
-----------------------------------------------------
Add ``=`` specifier to f-strings. ``f'{expr=}'`` expands
to the text of the expression, an equal sign, then the repr of the
evaluated expression. So::
x = 3
print(f'{x*9 + 15=}')
Would print ``x*9 + 15=42``.
所以它也适用于任意表达式。好的!
梦想:变量名中类似 JavaScript 的字典键
我发现 Python 在几乎所有方面都比 JavaScript 更好,但我越来越喜欢这个 JavaScript 功能:
let abc = 1
let def = 2
console.log({abc, def})
在 JavaScript 中工作是因为{abc, def}
扩展到{abc: 1, def: 2}
。这真是太棒了,除了日志记录之外,它还在代码的其他地方被广泛使用。
目前在 Python 中不可能很好地实现,除了locals
: Python 变量作为 dict 的键