by binarycrusader · 13y ago · 0 comments
condition and truecase or falsecase
See "Boolean operations" and "Conditional expressions":
http://docs.python.org/3/reference/expressions.html#boolean-...
Nowadays you would do this: truecase if condition else falsecase
Personally, I prefer the brevity of the older syntax, but to each his own. However, I understand the "ternary" expression was added because the older syntax was error-prone:
http://mail.python.org/pipermail/python-dev/2005-September/0... http://www.python.org/dev/peps/pep-0308/
truecase if condition else falsecase
which is so inside-out that it hurts.
It didn't occur to me to use the boolean style - I guess I think of that as a perl idiom, and assumed that it would get sneered at in py-land
$CONDITION && $TRUE_CASE || $FALSE_CASE
used as a Perl idiom in lieu of: $CONDITION ? $TRUE_CASE : $FALSE_CASE
I can't claim to have worked with Perl much prior to Perl 5.6/5.8 though, so it may have been an older idiom (though I doubt it as one of Perl's influences was Awk, which also has a C ternary operator, IIRC).That said, using truthy/falsey values in Python doesn't seem super common. I surprised a couple of interviewers by doing that while implementing some tree manipulation functions.