The usage of {} in Python f-strings is well known to execute pieces of code and give the result in string
format (some tutorials here). However, what does the ‘=‘ at the end of the expression mean?
JavaScript
x
4
1
log_file = open("log_aug_19.txt", "w")
2
console_error = '...stuff...' # the real code generates it with regex
3
log_file.write(f'{console_error=}')
4
Advertisement
Answer
This is actually a brand-new feature as of Python 3.8.
Added an
=
specifier to f-strings. An f-string such asf'{expr=}'
will expand to the text of the expression, an equal sign, then the representation of the evaluated expression.
Essentially, it facilitates the frequent use-case of print-debugging, so, whereas we would normally have to write:
JavaScript
1
2
1
f"some_var={some_var}"
2
we can now write:
JavaScript
1
2
1
f"{some_var=}"
2
So, as a demonstration, using a shiny-new Python 3.8.0 REPL:
JavaScript
1
4
1
>>> print(f"{foo=}")
2
foo=42
3
>>>
4