Created
January 9, 2011 21:59
-
-
Save tehmaze/772077 to your computer and use it in GitHub Desktop.
Python Ternary operator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Ternary(object): | |
''' | |
Ternary-ish emulation, it looks like C-style ternary operator:: | |
x = a ? b : c | |
In Python we would write:: | |
>>> x = a and b or c | |
Or (rather than above, this is safe for returning Falsy values for b):: | |
>>> x = (a and [b] or [c])[0] | |
Or:: | |
>>> x = b if a else c | |
Or:: | |
>>> x = lambda i: (b, c)[not a] | |
Or:: | |
>>> if a: | |
... x = b | |
... else: | |
... x = c | |
Now we can also write:: | |
>>> x = ternary[a:b:c] | |
''' | |
__getitem__ = lambda s, sl: (sl.start and sl.stop, not sl.start and sl.step)[not sl.start] | |
# Actual "operation", we can only work with an instance | |
T = Ternary() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment