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)
| File | Size | Uploaded | |
|---|---|---|---|
| ternary-0.1.tar.gz | 1.3 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| 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 |