[Python-Dev] Subtle difference between f-strings and str.format()
Serhiy Storchaka
storchaka at gmail.com
Thu Mar 29 14:30:42 EDT 2018
29.03.18 13:17, Jeff Allen пише:
> '{1} {0}'.format(a(), b()) # E1
>
> f'{b()}{a()}' # E2
>
>
> I think I would be very surprised to find b called before a in E1
> because of the general contract on the meaning of method calls. I'm
> assuming that's what an AST-based optimisation would do? There's no
> reason in E2 to call them in any other order than b then a and the
> documentation tells me they are.
I was going to optimize only formatting with implicit references. '{}
{}' but not '{1} {0}' and either not '{0} {1}'. This guaranties in-order
computation and referencing every subexpression only once. I don't have
a goal of converting every string formatting, but only the most common
and the most simple ones.
If go further, we will need to add several new AST nodes (like for
comprehensions).
More information about the Python-Dev
mailing list