If Python does not have a ternary conditional operator, is it possible to simulate one using other language constructs?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
You can index into a tuple:
test
needs to return True or False.It might be safer to always implement it as:
or you can use the built-in
bool()
to assure a Boolean value:Yes.
you can do this :-
[condition] and [expression_1] or [expression_2] ;
Example:-
print(number%2 and "odd" or "even")
This would print "odd" if the number is odd or "even" if the number is even.
The result :- If condition is true exp_1 is executed else exp_2 is executed.
Note :- 0 , None , False , emptylist , emptyString evaluates as False. And any data other than 0 evaluates to True.
Here's how it works:
if the condition [condition] becomes "True" then , expression_1 will be evaluated but not expression_2 . If we "and" something with 0 (zero) , the result will always to be fasle .So in the below statement ,
The expression exp won't be evaluated at all since "and" with 0 will always evaluate to zero and there is no need to evaluate the expression . This is how the compiler itself works , in all languages.
In
the expression exp won't be evaluated at all since "or" with 1 will always be 1. So it won't bother to evaluate the expression exp since the result will be 1 anyway . (compiler optimization methods).
But in case of
The second expression exp2 won't be evaluated since
True and exp1
would be True when exp1 isn't false .Similarly in
The expression exp1 won't be evaluated since False is equivalent to writing 0 and doing "and" with 0 would be 0 itself but after exp1 since "or" is used, it will evaluate the expression exp2 after "or" .
Note:- This kind of branching using "or" and "and" can only be used when the expression_1 doesn't have a Truth value of False (or 0 or None or emptylist [ ] or emptystring ' '.) since if expression_1 becomes False , then the expression_2 will be evaluated because of the presence "or" between exp_1 and exp_2.
In case you still want to make it work for all the cases regardless of what exp_1 and exp_2 truth values are, do this :-
[condition] and ([expression_1] or 1) or [expression_2] ;
Many programming languages derived from
C
usually have the following syntax of ternary conditional operator:So, firstly it evaluates the condition. If it returns
True
, expression1 will be evaluated to give the result, otherwise expression2 will be evaluated. Due to Lazy Evaluation mechanics – only one expression will be executed.Here are some examples (conditions will be evaluated from left to right):
Ternary operators can be changed:
The following one is the same as previous one:
Hope this helps.
@up:
Unfortunately, the
solution doesn't have short-circuit behaviour; thus both falseValue and trueValue are evaluated regardless of the condition. This could be suboptimal or even buggy (i.e. both trueValue and falseValue could be methods and have side-effects).
One solution to this would be
(execution delayed until the winner is known ;)), but it introduces inconsistency between callable and non-callable objects. In addition, it doesn't solve the case when using properties.
And so the story goes - choosing between 3 mentioned solutions is a trade-off between having the short-circuit feature, using at least python 2.5 (IMHO not a problem anymore) and not being prone to "trueValue-evaluates-to-false" errors.
For Python 2.5 and newer there is a specific syntax:
In older Pythons a ternary operator is not implemented but it's possible to simulate it.
Though, there is a potential problem, which if
cond
evaluates toTrue
andon_true
evaluates toFalse
thenon_false
is returned instead ofon_true
. If you want this behavior the method is OK, otherwise use this:which can be wrapped by:
and used this way:
It is compatible with all Python versions.