[Python-Dev] Subtle difference between f-strings and str.format()

Tim Peters tim.peters at gmail.com
Wed Mar 28 14:54:20 EDT 2018


[Tim]
> Same top-level point, though: [for evaluating `f() and g()`]:
>
> result = f()
> if bool(result):
>     result = g()

Ah, I think I see your point now.  In the _context_ of `if f() and
g()`, the dumbest possible code generation would do the above, and
then go on to do

if bool(result):
    ....

If in fact `f()` returned a false-like value, an optimizer could note
that `bool(result)` had already been evaluated and skip the redundant
evaluation.  I think that's fine either way:  what the language
guarantees is that `f()` will be evaluated exactly once, and `g()` no
more than once, and that's all so regardless.


More information about the Python-Dev mailing list

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant