News for hackers. Stuff that matters.
Re:Brilliant or insane code? by hknozcan on Sunday September 01, 2013 @07:42AM (#6309944)
We all love short and fast. But this is definitely an interesting approach. I'd love to see similar approaches to problems if you guys can point out to some.
Re:Brilliant or insane code? by Jach on Sunday September 01, 2013 @08:45AM (#6310146)
You might like the Python Infrequently Answered Questions: http://norvig.com/python-iaq.html (Even if it's a bit old.)
Re:Brilliant or insane code? by aidos on Monday September 02, 2013 @03:24AM (#6313877)
Thanks for that. An enjoyable read. Shame it's so out of date now.
Re:Brilliant or insane code? by StavrosK on Sunday September 01, 2013 @07:55AM (#6310005)
I would also, I love this sort of thing. How about:

    >>> some_boolean = False
    >>> ["Thing 1", "Thing 2"][some_boolean]
    "Thing 1"
Re:Brilliant or insane code? by icebraining on Sunday September 01, 2013 @08:27AM (#6310096)
Yeah, but unlike the hack in the article, the more readable version of that code,

  "Thing 1" if some_boolean else "Thing 2"
is also almost twice as fast(775 vs 1340 ns, on my machine).
Re:Brilliant or insane code? by StavrosK on Sunday September 01, 2013 @08:42AM (#6310141)
I was more posting it for the "cute hack" value, rather than speed. Speed was significant in the post because the author needed their library to be fast.
Re:Brilliant or insane code? by _ZeD_ on Sunday September 01, 2013 @08:28AM (#6310101)
the problem with this expression is that it doesn't "short circuit". Both "Thing 1" and "Thing 2" (in this cases object, but the can be function calls) are evaluated before the "some_boolean" usage.
Re:Brilliant or insane code? by TheLoneWolfling on Sunday September 01, 2013 @08:56AM (#6310170)
That can be an advantage in some cases, however. Not generally, though.

In particular, if you want the side effects of both operations. Trying to write multiple things while checking if any of them failed, for instance.

Re:Brilliant or insane code? by hknozcan on Sunday September 01, 2013 @08:49AM (#6310153)
Short but I doubt this will be faster as noted.