3 ms·
I would also, I love this sort of thing. How about: >>> some_boolean = False >>> ["Thing 1", "Thing 2"][some_boolean] "Thing 1"
by StavrosK 13y ago
I would also, I love this sort of thing. How about:
>>> some_boolean = False
>>> ["Thing 1", "Thing 2"][some_boolean]
"Thing 1"
- icebraining 13y agoYeah, 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).
- StavrosK 13y agoI 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.
- _ZeD_ 13y agothe 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.
- TheLoneWolfling 13y agoThat 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.
- hknozcan 13y agoShort but I doubt this will be faster as noted.