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 details)

Uploaded Source

Built Distribution

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

Uploaded Source

File details

Details for the file ternary-0.1.tar.gz.

File metadata

  • Download URL: ternary-0.1.tar.gz
  • Upload date:
  • Size: 1.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for ternary-0.1.tar.gz
Algorithm Hash digest
SHA256 a5ca7ef31724847b31db28fde4b224d32eec0b98bf45ccb77cb1a44f6ad02c8f
MD5 7bee4728ed3b7a6f7a32dfae7de0a496
BLAKE2b-256 81921c1e2febb6c54b07ac7c9ea98a5e3a1b91aa870fd2a5cb935d3ff0f0240b

See more details on using hashes here.

File details

Details for the file ternary-0.1-py2.6.egg.

File metadata

  • Download URL: ternary-0.1-py2.6.egg
  • Upload date:
  • Size: 2.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for ternary-0.1-py2.6.egg
Algorithm Hash digest
SHA256 1eb391437c61e23ec9681f77283bed086aac9b69b4299e8a9cd7caded46121f8
MD5 606a5c55f9eb6f833dcddca318fcfdaf
BLAKE2b-256 e99cb9d4303ac889c1a980a10bd83bcb118fbfd0980b0cd00356c54f68ed63ac

See more details on using hashes here.

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