Re:Adding Syntax to the CPython Interpreter
by 333c on Saturday October 19, 2024 @11:11AM
(#41888248)
Yeah, it would be confusing. In Ruby, this would leave spam unmodified.
[ Parent
| on HN ]
Re:Adding Syntax to the CPython Interpreter
by 8n4vidtmkvmk on Saturday October 19, 2024 @12:22PM
(#41888731)
That's even more confusing. There's an =, I expect an assignment to happen. Maybe we just leave this one alone.
Re:Adding Syntax to the CPython Interpreter
by 333c on Saturday October 19, 2024 @02:45PM
(#41889718)
In Ruby, the `if [condition]` modifier at the end of a line is used for more cases than just assignment. For example, `return if s.blank?` and `raise "invalid" if input.length > 100`. In Ruby, this pattern makes it clear that the statement is only executed if the condition is met.
I'm not advocating for this feature to be added to Python, just explaining why it's not confusing in Ruby.
Re:Adding Syntax to the CPython Interpreter
by mypalmike on Saturday October 19, 2024 @03:14PM
(#41889944)
Though I used it for 2 years of it as my primary language at work, I never quite got used to Ruby's quirky idioms like this. It just reads badly to me, in terms of quickly understanding code flow, to have statements that start with "raise" or "return" which might not raise or return. Similar to the up-thread comment about assignment.
Re:Adding Syntax to the CPython Interpreter
by trehalose on Saturday October 19, 2024 @01:50PM
(#41889326)
Perhaps using a parenthesized assignment expression with the walrus operator would be unambiguous:
(spam := eggs) if bar
I think that seems reasonable? It would act just the same as it already does with an explicit `else None`, if I'm not mistaken. I don't find it beautiful though.