|
|
Subscribe / Log in / New account

Python 3.12 released

Python 3.12 released

Posted Oct 3, 2023 14:00 UTC (Tue) by anarcat (subscriber, #66354)
Parent article: Python 3.12 released

I'm not a fan of:

f"This is the playlist: {", ".join(songs)}"
To me, this reads as f"foo" then a comma, then another string. In fact, this is a parse error in earlier Python, but I think that was actually a *good* thing. Now it seems the parser will just start huting for a matching bracket in a random quote following the f-string, isn't that error-prone if not downright dangerous?


to post comments

Python 3.12 released

Posted Oct 3, 2023 16:07 UTC (Tue) by NYKevin (subscriber, #129325) [Link] (5 responses)

> Now it seems the parser will just start huting for a matching bracket in a random quote following the f-string, isn't that error-prone if not downright dangerous?

No, it isn't, because there's no "in a random quote" rule. The matching bracket does not have to be quoted, and in fact it is an error to quote it. Officially, f"FOO{expr}BAR" is parsed into these parts:

f"
FOO
{expr}
BAR
"

expr is interpreted as if it is not inside of any quoted context (the braces temporarily "cancel" the enclosing quotes). But in practice, you could just as validly interpret it like this:

f"FOO{
expr
}BAR"

In other words, you *could* think of the braces as matching (and temporarily "closing") the quotes, even though they formally don't, because it turns out that that produces the same overall behavior (provided the braces are also properly balanced). This also works for nested contexts like f"FOO{f"bar{"BAZ"}qux"}THE_END":

f"FOO{
f"bar{
"BAZ"
}qux"
}THE_END"

(This is a very silly example, because you could just concatenate all of the string literals and write one big string literal instead.)

However, the opening and closing quotes do have to match, so you can't write f"FOO{expr}BAR' (the single quote does not match the double quote). If you want to think of the left brace as temporarily closing the quote, then the right brace should be considered to reopen the same quote (rather than opening a brand-new quote).

Python 3.12 released

Posted Oct 3, 2023 16:14 UTC (Tue) by anarcat (subscriber, #66354) [Link]

Well sure, I understand how it works, but if you have to spell it out all the way like this, for me it means we lost something in the translation...

Python 3.12 released

Posted Oct 3, 2023 18:21 UTC (Tue) by Karellen (subscriber, #67644) [Link] (2 responses)

the braces temporarily "cancel" the enclosing quotes

I mean, I guess that's how quotes work in the sh command substitution syntax $(....), but that's shell. It feels... weird and icky to put that sort of thing in a "real" programming language like Python!

Python 3.12 released

Posted Oct 3, 2023 19:13 UTC (Tue) by NYKevin (subscriber, #129325) [Link] (1 responses)

Although PEP 701 does cite bash as inspiration, it also cites several "real" programming languages, including Ruby (🔥 take: Ruby is just Python with different syntax):

> Many languages that allow similar syntactic constructs (normally called “string interpolation”) allow quote reuse and arbitrary nesting. These languages include JavaScript, Ruby, C#, Bash, Swift and many others. The fact that many languages allow quote reuse can be a compelling argument in favour of allowing it in Python. This is because it will make the language more familiar to users coming from other languages.

Python 3.12 released

Posted Oct 3, 2023 22:11 UTC (Tue) by mathstuf (subscriber, #69389) [Link]

I just wish that some of the corner cases didn't exist (Python 3.11):

In [2]: f'a dict: { {1: 2, "a": 4}}'
Out[2]: "a dict: {1: 2, 'a': 4}"

So…`}}` is not an escape for `}`…sometimes?

In [6]: f'a dict: { {1: 2, "a":: 4} }'
Cell In[6], line 1
f'a dict: { {1: 2, "a":: 4} }'
^
SyntaxError: f-string: invalid syntax

I'm not sure why this column marker seems to care so much about the first key when the second key's `::` is the actual problem. Maybe it gets caught as a format specifier somehow?

Python 3.12 released

Posted Oct 3, 2023 18:25 UTC (Tue) by mathstuf (subscriber, #69389) [Link]

That's all well and good, but it seems that syntax highligher libraries will need to learn this. NeoVim 0.9.2 highlights this example as the string ending before that first `,`.

Python 3.12 released

Posted Oct 4, 2023 6:36 UTC (Wed) by epa (subscriber, #39769) [Link]

It would all be more readable with balanced quotation marks “ and ”

It's kind of a historical accident that most programming languages have ended up with the same character to open and close a quoted string.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds