
On 8/1/2015 2:25 PM, Steven D'Aprano wrote:
On Sat, Aug 01, 2015 at 01:43:49PM -0400, Eric V. Smith wrote:
With str.format, !s and !r are needed because you can't put the call to repr in str.format's very limited expression syntax. But since f-strings support arbitrary expressions, it's not needed.
Wait, did I miss something? Does this mean that f-strings will essentially be syntactic sugar for str(eval(s))?
f"[i**2 for i in sequence]"
f = lambda s: str(eval(s)) f("[i**2 for i in sequence]")
Well, it's somewhat more complex. It's true that:
sequence=[1,2,3] f"{[i**2 for i in sequence]}" '[1, 4, 9]'
But it's more complex when there are format specifiers and literals involved. Basically, the idea is that: f'a{expr1:spec1}b{expr2:spec2}c' is shorthand for: ''.join(['a', expr1.__format__(spec1), 'b', expr2.__format__(spec2), 'c']) The expressions can indeed be arbitrarily complex expressions. Because only string literals are supported, it just the same as if you'd written the expressions not inside of a string (as shown above). It's not like you're eval-ing user supplied strings. Eric.