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
History Node

0.1

Download files

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

Filename, size & hash SHA256 hash help File type Python version Upload date
ternary-0.1-py2.6.egg (2.2 kB) Copy SHA256 hash SHA256 Egg 2.6 Jul 22, 2010
ternary-0.1.tar.gz (1.3 kB) Copy SHA256 hash SHA256 Source None Jul 22, 2010

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