←back to thread

160 points leontrolski | 1 comments | | HN request time: 0.196s | source
Show context
cpburns2009 ◴[] No.41887804[source]
For all of the syntax features Python has been adding over the years, this would be a nice enhancement: making the "else None" optional in the ternary if-expression. E.g.,

    spam = eggs if bar
    # vs
    spam = eggs if bar else None
replies(3): >>41887917 #>>41887943 #>>41888372 #
genter ◴[] No.41887917[source]
So if "else None" is omitted, if bar is false, then does spam == None or is it unmodified? The former is what I think you want, but that would be very confusing.
replies(2): >>41888248 #>>41889001 #
333c ◴[] No.41888248[source]
Yeah, it would be confusing. In Ruby, this would leave spam unmodified.
replies(1): >>41888731 #
8n4vidtmkvmk ◴[] No.41888731[source]
That's even more confusing. There's an =, I expect an assignment to happen. Maybe we just leave this one alone.
replies(2): >>41889326 #>>41889718 #
1. trehalose ◴[] No.41889326[source]
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.