No ternary operator in Python

I find it surprising that, at least by the unofficial tally, Python is not getting a ternary if-else operator. One of the first things you learn in Dive Into Python is that you can use foo and bar or baz for C's ?: operator--except it breaks if bar is false (eg None or 0).

At least, I think that, but then I notice I haven't used it in Perl much at all. So maybe it's just as well.

TrackBack

Listed below are links to weblogs that reference No ternary operator in Python:

» Ternary operator in Python? from markpasc.org
Open source is confusing. [Read More]

Comments

comment

I use the ternary operator a lot in JavaScript, because a lot of that is one-liners (onclick handlers, bookmarklets, whatever). Where I don’t have to condense code, I very rarely use a ternary operator anyway, so I don’t miss it in Python.

comment

Yeah, I like the conditional x ? A : B also.

ALSO python allows x += 1 why not allow x++ or maybe ++x for increments? x++ is very concise and direct. Same w/ x—

comment

I heard += does some crazy things (I thought from Adam Vandenberg but I can’t find it right now), though I still use it on occasion. The increment and decrement are weird because those would seem to allow those as expressions instead of statements (which normal assignment and presumably += are required to be). I can see the argument they’re less “Pythonic” (especially after seeing what a mess an ugly list comprehension was, earlier today).