[Python-Dev] PEP 498: Literal String Interpolation is ready for pronouncement (original) (raw)

haypo s victor.stinner at gmail.com
Sat Sep 5 11:10:12 CEST 2015


2015-09-05 5:01 GMT+02:00 Guido van Rossum <guido at python.org>:

And I'm ready to accept it. I'll wait until Tuesday night (Monday's a holiday in the US) in case anything unforeseen comes up, but this is really the Last Call for this PEP.

Would it be possible to specify a subset of the Python language allowed in f-string? For example, import or lambda should not be used in a f-string. I'm not convinced that a loop or list/dict/set-comprehension is a good idea neither.

I would prefer to keep as much code as possible outside f-string because:

For example, for me it's a common practice to write a complex list-comprehension on two lines for readability:

newlist = [very_complex_expression(item) for item in oldlist]

sorry, it's hard to indent correctly in a mail client, especially Gmail

Well, I'm not convinced that we need a larger subset than what is allowed currently in str.format(), simple expressions like: obj.attr, obj[index], etc.

I recall horrible examples in the previous mail threads showing how much complex code you can put inside f-string.

Even the following example from the PEP seems too complex to me: print(f"Usage: {sys.argv[0]} [{'|'.join('--'+opt for opt in valid_opts)}]", file=sys.stderr)

Oh, first I read [...] as a list-comprehension :-p But it's part of the output string, not of the Python code...

I prefer to build the second parameter outside the f-string: opts = '|'.join('--'+opt for opt in valid_opts) print(f"Usage: {sys.argv[0]} [{opts}]", file=sys.stderr)

f-string with complex code remembers me PHP where it was possible to mix PHP and HTML. I have bad memories such... code? template? (I don't know how to cal them). Text editors and me were unable to identify quickly the beginning and end of the code. We have a similar issue with Python unit test embedding Python code to run subprocesses. My text editor vim is unable to identify if the current code is the outter code or the code embedded in a long triple-quoted string (especially when you open the file at the embedded code).

Maybe we should start with a simple f-string, play with it during one cycle (Python 3.6), and then discuss again if it's necessary to extend the allowed Python expresions inside f-string?

If you really want to allow any Python inside a f-string, can you please at least explain in th PEP why you consider that it's a good thing?

IMHO the main advantage of allowing expressions inside f-string is that it adds something really new compared to the current str.format() method. Without it, f-string are less interesting.

Victor



More information about the Python-Dev mailing list