Python 3.12 released
Posted Oct 3, 2023 10:02 UTC (Tue)
by whoami (guest, #166229)
[Link] (5 responses)
Posted Oct 3, 2023 10:10 UTC (Tue)
by mb (subscriber, #50428)
[Link] (2 responses)
Posted Oct 3, 2023 12:45 UTC (Tue)
by excors (subscriber, #95769)
[Link] (1 responses)
Posted Oct 4, 2023 4:20 UTC (Wed)
by alexeiz (guest, #95579)
[Link]
Posted Oct 3, 2023 13:03 UTC (Tue)
by Wol (subscriber, #4433)
[Link]
Cheers,
Posted Oct 3, 2023 13:33 UTC (Tue)
by farnz (subscriber, #17727)
[Link]
Because, when you rip off all the layers of meaning, "political" means "the way people treat labelled groups of people", and programming languages are all about the way people treat one labelled group of people - programmers.
Posted Oct 3, 2023 14:00 UTC (Tue)
by anarcat (subscriber, #66354)
[Link] (7 responses)
Posted Oct 3, 2023 16:07 UTC (Tue)
by NYKevin (subscriber, #129325)
[Link] (5 responses)
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"
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{
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{
(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).
Posted Oct 3, 2023 16:14 UTC (Tue)
by anarcat (subscriber, #66354)
[Link]
Posted Oct 3, 2023 18:21 UTC (Tue)
by Karellen (subscriber, #67644)
[Link] (2 responses)
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!
Posted Oct 3, 2023 19:13 UTC (Tue)
by NYKevin (subscriber, #129325)
[Link] (1 responses)
> 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.
Posted Oct 3, 2023 22:11 UTC (Tue)
by mathstuf (subscriber, #69389)
[Link]
In [2]: f'a dict: { {1: 2, "a": 4}}'
So…`}}` is not an escape for `}`…sometimes?
In [6]: f'a dict: { {1: 2, "a":: 4} }'
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?
Posted Oct 3, 2023 18:25 UTC (Tue)
by mathstuf (subscriber, #69389)
[Link]
Posted Oct 4, 2023 6:36 UTC (Wed)
by epa (subscriber, #39769)
[Link]
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.
Python 3.12 released
Python 3.12 released
Python 3.12 released
Python 3.12 released
Python 3.12 released
Wol
Python 3.12 released
I'm not a fan of:
Python 3.12 released
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?
Python 3.12 released
FOO
{expr}
BAR
"
expr
}BAR"
f"bar{
"BAZ"
}qux"
}THE_END"
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
Python 3.12 released
the braces temporarily "cancel" the enclosing quotes
Python 3.12 released
Python 3.12 released
Out[2]: "a dict: {1: 2, 'a': 4}"
Cell In[6], line 1
f'a dict: { {1: 2, "a":: 4} }'
^
SyntaxError: f-string: invalid syntax
Python 3.12 released
Python 3.12 released