Why does python not need the three-object operator and switch?
For ternary operator, python can be replaced by conditional expressions.
For example, for x <5? 1-0 can be implemented in the following way
1if x <5 else 0
Note: conditional expressions was introduced before python 2.5, so the above Code is only applicable to versions 2.5 and later.
For versions earlier than 2.5, the following form can be used:
X <5and1or 0
We can use dictionary to implement the switch. See the following example.
>>> Def switch (choice): return dict (enumerate (range (4) [choice] >>> switch (1) >>> switch (0) values = {value1: do_something1, value2: do_something2 ,... valueN: do_somethingN,} values. get (var, do_default_something )()
The above is the small series for everyone to talk about why python does not need the three-object operator and switch all the content, I hope you can support a lot of help house ~