<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Mon, Jul 20, 2015 at 9:52 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 07/20/2015 03:22 PM, Guido van Rossum wrote:<br>
<br>
>     > So the idea is that<br>
>     > f'x:{a.x} y:{y}'<br>
>     > would translate to bytecode that does:<br>
>     > 'x:{a.x} y:{y}'.format(a=a, y=y)<br>
>     ><br>
>     > Correct?<br>
><br>
>     That's exactly what I had in mind, at least. Indexing is supported<br>
>     in format strings too, so f'{a[1]}' also becomes<br>
>     '{a[1]}'.format(a=a), but I don't think there are any other strange<br>
>     cases here. I would vote for f'{}' or f'{0}' to just be a SyntaxError.<br>
><br>
><br>
> +1 on that last sentence. But I prefer a slightly different way of<br>
> implementing (see my reply to Eric).<br>
<br>
</span>Right. And following up here to that email:<br>
<span class=""><br>
> I was more thinking of translating that specific example to<br>
><br>
>     'x:{} y:{}'.format(a.x, y)<br>
><br>
> which avoids some of the issues your example is trying to clarify.<br>
<br>
</span>That is better. The trick is converting the string "a.x" to the<br>
expression a.x, which should be easy enough at compile time.<span class=""><br></span></blockquote><div><br></div><div>I wonder if we could let the parser do this? consider f'x:{ as one token and so on?<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">
> It would still probably be best to limit the syntax inside {} to exactly<br>
> what regular .format() supports, to avoid confusing users.<br>
<br>
</span>The expressions supported by .format() are limited to attribute access<br>
and "indexing". We just need to enforce that same restriction here.<br>
<span class=""><br>
> Though the consistency argument can be played both ways -- supporting<br>
> absolutely anything that is a valid expression would be more consistent<br>
> with other places where expressions occur. E.g. in principle we could<br>
> support operators and function calls here.<br>
<br>
</span>It would be easiest to not restrict the expressions, but then we'd have<br>
to maintain that restriction in two places.<br>
<br>
And now that I think about it, it's somewhat more complex than just<br>
expanding the expression. In .format(), this:<br>
'{a[0]}{b[c]}'<br>
is evaluated roughly as<br>
format(a[0]) + format(b['c'])<br></blockquote><div><br></div><div>Oooh, this is very unfortunate. I cannot support this. Treating b[c] as b['c'] in a "real" format string is one way, but treating it that way in an expression is just too weird.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
So to be consistent with .format(), we have to fully parse at least the<br>
indexing out to see if it looks like a constant integer or a string.<br>
<br>
So given that, I think we should just support what .format() allows,<br>
since it's really not quite as simple as "evaluate the expression inside<br>
the braces".<span class=""><br></span></blockquote><div><br></div><div>Alas. And this is probably why we don't already have this feature.<br> <br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">
> Not sure what you mean by "implicit merging" -- if you mean literal<br>
> concatenation (e.g. 'foo' "bar" == 'foobar') then I think it should be<br>
> allowed, just like we support mixing quotes and r''.<br>
<br>
</span>If I understand it, I think the concern is:<br>
<br>
f'{a}{b}' 'foo{}' f'{c}{d}'<br>
<br>
would need to become:<br>
f'{a}{b}foo{{}}{c}{d}'<br>
<br>
So you have to escape the braces in non-f-strings when merging strings<br>
and any of them are f-strings, and make the result an f-string. But I<br>
think that's the only complication.<br>
</blockquote></div><br></div><div class="gmail_extra">That's possible; another possibility would be to just have multiple .format() calls (one per f'...') and use the + operator to concatenate the pieces.<br clear="all"></div><div class="gmail_extra"><br>-- <br><div class="gmail_signature">--Guido van Rossum (<a href="http://python.org/~guido" target="_blank">python.org/~guido</a>)</div>
</div></div>