3 ms·
“=“ has been a great addition to F-strings. It allows you to reduce duplication for labeled output. For example: count = 10 size = 1000032 area = 3.14159
by pbourke 4y ago
“=“ has been a great addition to F-strings. It allows you to reduce duplication for labeled output. For example:
count = 10
size = 1000032
area = 3.14159 * (32 ** 2)
print(f"{count=} {size=:,} {area=:,.2f}")
will output this
count=10 size=1,000,032 area=3,216.99
- ajross 4y agoCounterargument: "=" is YAMLization. It prioritizes concision and an abstracted ideal of "readability"[1] at the expense of mental model and ease of use. Every non-obvious feature like this has cost. Every user who sees this for the first time is going to be confused. Every time a language or syntax adds a "shorthand" feature like this it makes itself less accessible. And in this case I think the value is really pretty low. That particular "=" syntax is really mostly just useful for dumping runtime state at debug time. And the world is filled with tools that can do that for you with even more concision. [1] Which doesn't really correspond to general comprehension. It means something more like "readability for experts".
- pbourke 4y ago> It prioritizes concision and an abstracted ideal of "readability"[1] at the expense of mental model and ease of use. That statement applies to all format strings, which are by necessity a small internal DSL. For the = syntax, the input is copied directly to the output with the value of the variable appended. Within the realm of language features, sure it's one more chocolate wafer of syntax. Within the realm of format strings, it's certainly more straightforward than the type conversion and padding syntax. > That particular "=" syntax is really mostly just useful for dumping runtime state at debug time. In scientific / ML development that's like 80% of the work.
- deleted 4y ago[deleted]
- pantsforbirds 4y agoIt's an extremely useful pattern for me when logging to be honest.
- pantsforbirds 4y agoThis is honestly my favorite formatting feature. I use it a lot in debug/info logs and its honestly my first go to when debugging something trivial enough that I don't want to spin up the full debugger.
- blooalien 4y agoI use this regularly, combined with Wil McGugan's `rich.print` for easy fancy colorized output. f-strings are one of my favorite things by far to happen to Python in recent memory, along with the robust Unicode support and fancy new `async` stuff. :)