News for hackers. Stuff that matters.
Re:Why 1 && 2 == 2 by binarycrusader on Thursday July 18, 2013 @11:47PM (#6068218)
For Python, a common idiom is to do this:

condition and truecase or falsecase

See "Boolean operations" and "Conditional expressions":

http://docs.python.org/3/reference/expressions.html#boolean-...

Re:Why 1 && 2 == 2 by Xixi on Thursday July 18, 2013 @11:56PM (#6068245)
This idiom is however out of flavor, unless you need to support python 2.4.

Nowadays you would do this: truecase if condition else falsecase

Re:Why 1 && 2 == 2 by binarycrusader on Friday July 19, 2013 @12:45AM (#6068375)
Heh; my "age" is starting to show. The first version of Python I learned was Python 2.4 and it was only a few years ago I finally moved to 2.6. Skipping 2.7 and moving to 3.3+ soon.

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/

Re:Why 1 && 2 == 2 by jes5199 on Thursday July 18, 2013 @11:59PM (#6068255)
ah! I've been trying to write Python lately (I'm more of a Rubyist) and the internet suggested:

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

Re:Why 1 && 2 == 2 by pyre on Friday July 19, 2013 @01:23AM (#6068464)
I've never seen:

  $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.

Re:Why 1 && 2 == 2 by comex on Friday July 19, 2013 @12:25AM (#6068328)
It was mostly used before the creation of the ternary operator in Python 2.5.
Re:Why 1 && 2 == 2 by cheald on Friday July 19, 2013 @12:56AM (#6068404)
This is how you do it in Lua, as well, since it doesn't have a ternary operator.