Skip to main content

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

This version

0.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for ternary, version 0.1
Filename, size File type Python version Upload date Hashes
Filename, size ternary-0.1-py2.6.egg (2.2 kB) File type Egg Python version 2.6 Upload date Hashes View hashes
Filename, size ternary-0.1.tar.gz (1.3 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page