[Python-ideas] String interpolation for all literal strings

Eric V. Smith eric at trueblade.com
Sat Aug 8 23:34:03 CEST 2015


On 8/8/2015 1:23 PM, Jonathan Slenders wrote:
> Why don't we allow any possible expression to be used in the context of
> a decorator? E.g. this is not possible.
> 
> @a + b
> def function():
>     pass
> 
> While these are:
> 
> @a(b + c)
> @a.b
> @a.b.c
> def function():
>     pass
> 
> I guess there we also had a discussion about whether or not to limit the
> grammar, and I guess we had a reason.
> 
> I don't like the idea to give the user too much freedom in f-string. A
> simple expression like addition, ok. But no comprehension, lambdas,
> etc... It's impossible to go back if this turns out badly, but we can
> always add more freedom later on.

Yes, there's been a fair amount of discussion on this. The trick would
be finding a place in the grammar that allows enough, but not too much
expressiveness. I personally think it should just be a code review item.
Is there really anything wrong with:

>>> msg = 'apple'
>>> f'The sign said "{msg.upper()}".'
'The sign said "APPLE".'

> One more coments after reading the PEP:
> - I don't like that double braces are replaced by a single brace. Why
> not keep backslash \{  \} for the literals. In the PEP we have '{...}'
> for variables. (Instead of '\{...}') So that works fine.

I kept the double braces to maximize compatibility with str.format.

Eric.


More information about the Python-ideas mailing list