<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Mon, Jul 20, 2015 at 7:57 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 01:25 PM, Guido van Rossum wrote:<br>
> Perhaps surprisingly, I find myself leaning in favor of the<br>
> f'...{var}...' form. It is explicit in the variable name.<br>
><br>
> Historically, the `x` notation as an alias for repr(x) was meant to play<br>
> this role -- you'd write '...' + `var` + '...', but it wasn't brief<br>
> enough, and the `` are hard to see. f'...' is more explicit, and can be<br>
> combined with r'...' and b'...' (or both) as needed.<br>
<br>
</span>We didn't implement b''.format(), for a variety of reasons. Mostly to do<br>
with user-defined types returning unicode from __format__, if I recall<br>
correctly.<br></blockquote><div><br></div><div>Oh, I forgot that.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
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></blockquote><div><br></div><div>I was more thinking of translating that specific example to<br><br></div><div>    'x:{} y:{}'.format(a.x, y)<br><br></div><div>which avoids some of the issues your example is trying to clarify.<br><br></div><div>It would still probably be best to limit the syntax inside {} to exactly what regular .format() supports, to avoid confusing users.<br><br>Though the consistency argument can be played both ways -- supporting absolutely anything that is a valid expression would be more consistent with other places where expressions occur. E.g. in principle we could support operators and function calls here.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I think I could leverage _string.formatter_parser() to do this, although<br>
it's been a while since I wrote that. And I'm not sure what's available<br>
at compile time. But I can look into it.<br></blockquote><div><br></div><div>I guess that would mean the former restriction. I think it's fine.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
I guess the other option is to have it generate:<br>
'x:{a.x} y:{y}'.format_map(collections.ChainMap(globals(), locals(),<br>
__builtins__))<br>
<br>
That way, I wouldn't have to parse the string to pick out what variables<br>
are referenced in it, then have .format() parse it again.<span class="HOEnZb"></span><br></blockquote></div><br></div><div class="gmail_extra">No; I really want to avoid having to use globals() or locals() here.<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>