Ternary operation implementation
Project description
This is a simple hack using the slice operation to mimic C-style ternary operation:
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]
Usage
Using the slice operation:
>>> value = ternary[condition:true_result:false_result]
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
ternary-0.1.tar.gz
(1.3 kB
view hashes)
Built Distribution
ternary-0.1-py2.6.egg
(2.2 kB
view hashes)