[Python-ideas] Briefer string format

Guido van Rossum guido at python.org
Mon Jul 20 21:22:55 CEST 2015


(Our posts crossed, to some extent.)

On Mon, Jul 20, 2015 at 8:41 PM, Steve Dower <Steve.Dower at microsoft.com>
wrote:

> Eric V. Smith wrote:
> > On 07/20/2015 01:25 PM, Guido van Rossum wrote:
> >> Perhaps surprisingly, I find myself leaning in favor of the
> >> f'...{var}...' form. It is explicit in the variable name.
>


> [...]
>


> > So the idea is that
> > f'x:{a.x} y:{y}'
> > would translate to bytecode that does:
> > 'x:{a.x} y:{y}'.format(a=a, y=y)
> >
> > Correct?
>
> That's exactly what I had in mind, at least. Indexing is supported in
> format strings too, so f'{a[1]}' also becomes '{a[1]}'.format(a=a), but I
> don't think there are any other strange cases here. I would vote for f'{}'
> or f'{0}' to just be a SyntaxError.
>

+1 on that last sentence. But I prefer a slightly different way of
implementing (see my reply to Eric).


> I briefly looked into how this would be implemented and while it's not
> quite trivial/localized, it should be relatively straightforward if we
> don't allow implicit merging of f'' strings. If we wanted to allow implicit
> merging then we'd need to touch more code, but I don't see any benefit from
> allowing it at all, let alone enough to justify seriously messing with this
> part of the parser.


Not sure what you mean by "implicit merging" -- if you mean literal
concatenation (e.g. 'foo' "bar" == 'foobar') then I think it should be
allowed, just like we support mixing quotes and r''.

-- 
--Guido van Rossum (python.org/~guido)
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20150720/57917be5/attachment.html>


More information about the Python-ideas mailing list