Python 3.8 f-string =
syntax
It has arrived!
#!/usr/bin/env python3
foo = 1
bar = 2
print(f"{foo=} {bar=}")
output:
foo=1 bar=2
Added in commit https://github.com/python/cpython/commit/9a4135e939bc223f592045a38e0f927ba170da32 “Add f-string debugging using ‘=’.” which documents:
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``.
so it also works for arbitrary expressions. Nice!
The dream: JavaScript-like dict keys from variable names
I find Python better than JavaScript in almost every sense, but I’ve grown to really like this JavaScript feature:
let abc = 1
let def = 2
console.log({abc, def})
works in JavaScript because {abc, def}
expands to {abc: 1, def: 2}
. This is just awesome, and gets used a lot in other places of the code besides logging.
Not possible nicely in Python currently except with locals
: Python variables as keys to dict