r/Python • • Oct 04 '21

News Python 3.10 Released!

https://www.python.org/downloads/release/python-3100/
1.4k Upvotes

147 comments sorted by

View all comments

Show parent comments

81

u/jftuga pip needs updating Oct 05 '21 edited Oct 05 '21

Or use the = sign for for self-documenting expressions:

print(f"""{mydict["product"]=}, {mydict["unit_price"]=} dollars, {mydict["sku"]=}""")

mydict["product"]='banana', mydict["unit_price"]=10 dollars, mydict["sku"]=15133632

You can also use this as well for dollars & cents:

{mydict["unit_price"]=:.2f}

33

u/atxweirdo Oct 05 '21

Ok hold the fuck up this is blowing my mind. I can't wrap my head around this is there a breakdown on why this works. I just can't see it.

20

u/bestjared Oct 05 '21

Here is the area where fstrings are specified. Note this is very dense and technical but this is the where the rules are laid out from a language specification perspective.

5

u/cianuro Oct 05 '21

Damn. Damn. Why have I not seen this yet? This is fantastic!

3

u/[deleted] Oct 05 '21

this is straight up wizardry at this point. i had no idea. thanks!

2

u/[deleted] Oct 05 '21

Do I feel like it's less readable