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 | RSS feed

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.

Source Distribution

ternary-0.1.tar.gz (1.3 kB view hashes)

Uploaded Source

Built Distribution

ternary-0.1-py2.6.egg (2.2 kB view hashes)

Uploaded Source

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page