Skip to main content

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]

Release files for ternary 0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ternary 0.1
File Size Uploaded
ternary-0.1.tar.gz 1.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for ternary 0.1
File Interpreter ABI Platform
ternary-0.1-py2.6.egg Legacy Egg format - - Details

Total release size: 3.5 kB

Release files / ternary-0.1.tar.gz

Download URL ternary-0.1.tar.gz
Size 1.3 kB
Tags Source
SHA-256 checksum
How to use checksums
a5ca7ef31724847b31db28fde4b224d32eec0b98bf45ccb77cb1a44f6ad02c8f
BLAKE2b-256 checksum
How to use checksums
81921c1e2febb6c54b07ac7c9ea98a5e3a1b91aa870fd2a5cb935d3ff0f0240b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / ternary-0.1-py2.6.egg

Download URL ternary-0.1-py2.6.egg
Size 2.2 kB
Tags Egg
SHA-256 checksum
How to use checksums
1eb391437c61e23ec9681f77283bed086aac9b69b4299e8a9cd7caded46121f8
BLAKE2b-256 checksum
How to use checksums
e99cb9d4303ac889c1a980a10bd83bcb118fbfd0980b0cd00356c54f68ed63ac
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

0.1 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page