Languages
Post #2678 · source on Telegram
The Trailing Comma Hill
Description
A "Change My Mind" meme shows a man at an outdoor table with a code line overlaid at the top: "files = get_files(dir, pattern),". The sign on the table reads, "A comma at the end of a command should be an error, not create a tuple." The joke targets Python's syntactic rule where a trailing comma can turn an expression into a tuple, creating subtle type surprises instead of an obvious syntax failure.
Use J and K for navigation
Comments
14Comment deleted
Python looked at a dangling comma and decided the parser was fine, but your type assumptions needed character development.
a compilation error Comment deleted
More like complication error Comment deleted
Computation error 🌚 Comment deleted
yes Comment deleted
third-degeneration Comment deleted
Python4?🤔 Comment deleted
this Comment deleted
At most a warning, but definitely not an error - it fits perfectly into the grammar model, and there aren't many reasons strong enough to force python to break its abstractions (which is its most wonderful aspect, in my opinion) Comment deleted
look at the walrus operator Comment deleted
I've skimmed through pep572 (the one regarding exactly that), and while I wouldn't have considered this addition necessary on my own, it doesn't seem to break any pre-established abstractions, and the developers' rationale is compelling enough for me to accept it Comment deleted
well, you sould be fine then with comma too, bc both would require to just put parenthesis around those expressions Comment deleted
That is quite reasonable, I agree. I guess the top-level tuple syntax is maybe allowed as one of the explicitly desired language features - tuple assignment (x, y = y, x), in particular. But you've got a good point nonetheless Comment deleted
oh crap, i thought that swap thing was magic, that's a good point too then Comment deleted