<div dir="ltr">Hm, without thinking too much about it I'd say it's okay to change the evaluation order. Can these optimizations be disabled with something like -O0?<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Mar 28, 2018 at 8:27 AM, Serhiy Storchaka <span dir="ltr"><<a href="mailto:storchaka@gmail.com" target="_blank">storchaka@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">There is a subtle semantic difference between str.format() and "equivalent" f-string.<br>
<br>
    '{}{}'.format(a, b)<br>
    f'{a}{b}'<br>
<br>
In the former case b is evaluated before formatting a. This is equivalent to<br>
<br>
    t1 = a<br>
    t2 = b<br>
    t3 = format(t1)<br>
    t4 = format(t2)<br>
    r = t3 + t4<br>
<br>
In the latter case a is formatted before evaluating b. This is equivalent to<br>
<br>
    t1 = a<br>
    t2 = format(t1)<br>
    t3 = b<br>
    t4 = format(t3)<br>
    r = t2 + t4<br>
<br>
In most cases this doesn't matter, but when implement the optimization that transforms the former expression to the the latter one ([1], [2]) we have to make a decision what to do with this difference.<br>
<br>
1. Keep the exact semantic of str.format() when optimize it. This means that it should be transformed into AST node different from the AST node used for f-strings. Either introduce a new AST node type, or add a boolean flag to JoinedStr.<br>
<br>
2. Change the semantic of f-strings. Make it closer to the semantic of str.format(): evaluate all subexpressions first than format them. This can be implemented in two ways:<br>
<br>
2a) Add additional instructions for stack manipulations. This will slow down f-strings.<br>
<br>
2b) Introduce a new complex opcode that will replace FORMAT_VALUE and BUILD_STRING. This will speed up f-strings.<br>
<br>
3. Transform str.format() into an f-string with changing semantic, and ignore this change. This is not new. The optimizer already changes semantic. Non-optimized "if a and True:" would call bool(a) twice, but optimized code calls it only once.<br>
<br>
[1] <a href="https://bugs.python.org/issue28307" rel="noreferrer" target="_blank">https://bugs.python.org/issue2<wbr>8307</a><br>
[2] <a href="https://bugs.python.org/issue28308" rel="noreferrer" target="_blank">https://bugs.python.org/issue2<wbr>8308</a><br>
<br>
______________________________<wbr>_________________<br>
Python-Dev mailing list<br>
<a href="mailto:Python-Dev@python.org" target="_blank">Python-Dev@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-dev" rel="noreferrer" target="_blank">https://mail.python.org/mailma<wbr>n/listinfo/python-dev</a><br>
Unsubscribe: <a href="https://mail.python.org/mailman/options/python-dev/guido%40python.org" rel="noreferrer" target="_blank">https://mail.python.org/mailma<wbr>n/options/python-dev/guido%<wbr>40python.org</a><br>
</blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature" data-smartmail="gmail_signature">--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)</div>
</div>