3 ms·
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-expres
by cpburns2009 2y ago
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
- genter 2y agoSo 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.
- 333c 2y agoYeah, it would be confusing. In Ruby, this would leave spam unmodified.
- 8n4vidtmkvmk 2y agoThat's even more confusing. There's an =, I expect an assignment to happen. Maybe we just leave this one alone.
- trehalose 2y agoPerhaps 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.
- 333c 2y agoIn 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.
- mypalmike 2y agoThough 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.
- entelechy0 2y ago[dead]
- TwentyPosts 2y agoDo we really need more syntactic sugar? Frankly, I am still confused why Python is going for a separate syntax for if expressions instead of just making its regular ifs into expressions
- cpfohl 2y agoThis is literally just a short tutorial helping people get into the cpython codebase. This feedback is off topic. Related to your comment, though: Python has had this syntax for a VERY long time.
- albertzeyer 2y agoThere is an advantage of having this as syntax, which is a difference in semantics, which you couldn't get if this would just be an expression, namely: x = func1() if something else func2() In this example, only func1() or func2() is being called, but not both.
- gray_-_wolf 2y agoI do not understand why x = if something: func1() else: func2() Would not work. At least that is what I imagine when it is said "make if an expression".
- __mharrison__ 2y agoThis doesn't work because the if statement is a statement. Statements don't evaluate to anything in Python, so you can't assign it to x.
- 8n4vidtmkvmk 2y agoHence "make it an expression"
- otabdeveloper4 2y ago
- kstrauser 2y agoI almost never use None as the second value here. From my POV, that would be new syntax for an unlikely situation.