<div dir="ltr">OK, fine, I'll say right now that I agree with Eric's arguments for full expressions.<br><br>(Though honestly the whole look of f-strings hasn't quite grown on me. I wish I could turn back the clock and make expression substitution a feature of all string literals, perhaps using \{...}, which IIRC I've seen in some other language.)<br></div><div class="gmail_extra"><br><div class="gmail_quote">On Tue, Aug 4, 2015 at 10:20 PM, Eric V. Smith <span dir="ltr"><<a href="mailto:eric@trueblade.com" target="_blank">eric@trueblade.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 8/4/2015 4:05 PM, Mike Miller wrote:<br>
> Hi,<br>
><br>
> In that message there was a logical step that I don't follow:<br>
><br>
>> For example:<br>
>> '{a.foo}'.format(a=b[c])<br>
>><br>
>> If we limit f-strings to just what str.format() string expressions can<br>
>> represent, it would be impossible to represent this with an f-string,<br>
>> without an intermediate assignment.<br>
><br>
>> For example:<br>
>> f'{a[2:3]:20d}'<br>
>><br>
>> We need to extract the expression "a[2:3]" and the format spec "20d". I<br>
>> can't just scan for a colon any more, I've got to actually parse the<br>
>> expression until I find a "}", ":", or "!" that's not part of the<br>
>> expression so that I know where it ends.<br>
><br>
> There was a solution to this that came up early in the discussion,<br>
> moving the identifier only:<br>
><br>
> f'{x}{y}' --> '{}{}'.format(x, y)<br>
> f'{x:10}{y[name]}' --> '{:10}{[name]}'.format(x, y)<br>
><br>
> I missed the part where this was rejected. As far as I can tell from<br>
> your message, it is because it would be hard to parse? But, it seems no<br>
> harder than other solutions. I've whipped up a simple implementation<br>
> below.<br>
><br>
<br>
</span>It's rejected because .format treats:<br>
'{:10}{[name]}'.format(x, y) --> format(x, '10') + format(y['name'])<br>
<br>
and we (for some definition of "we") would like:<br>
f'{x:10}{y[name]}' --> format(x, '10') + format(y[name])<br>
<br>
It's the change from y[name] to y['name'] that Guido rejected for<br>
f-strings. And I agree: it's unfortunate that str.format works this way.<br>
It would have been better just to say that the subscripted value must be<br>
a literal number for str.format, but it's too late for that.<br>
<br>
It's not hard to parse either way. All of the machinery exists to use<br>
either the str.format approach, or the full expression approach.<br>
<span class=""><br>
> Also, Guido sounds supportive of your general process, but to my<br>
> knowledge has not explicitly called for arbitrary expressions to be<br>
> included. Perhaps he could do that, or encourage us to find a more<br>
> conservative solution?<br>
<br>
</span>True, he hasn't definitively stated his approval for arbitrary<br>
expressions. I think it logically follows from our discussions. But if<br>
he'd like to rule on it one way or the other before I'm done with the<br>
PEP draft, that's fine with me. Or, we can just wait for the PEP.<br>
<br>
Personally, now that I have a working implementation that I've been<br>
using, I have to say that full expressions are pretty handy. And while I<br>
agree you don't want to be putting hyper-complicated dict comprehensions<br>
with lots of function calls into an f-string, the same can be said of<br>
many places we allow expressions.<br>
<span class=""><br>
> Sorry to be a pain, but I think this part is important to get right.<br>
<br>
</span>No problem. It's all part of the discussion.<br>
<span class="HOEnZb"><font color="#888888"><br>
Eric.<br>
</font></span><div class="HOEnZb"><div class="h5"><br>
_______________________________________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/codeofconduct/</a><br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature">--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)</div>
</div>