Ticket #1 (new defect)
Opened 3 years ago
Switch/Case flow-control in Python
| Reported by: | emuller | Owned by: | somebody |
|---|---|---|---|
| Priority: | major | Milestone: | |
| Component: | component1 | Version: | |
| Keywords: | Cc: |
Description
Python does not have Switch/Case? statements, and likely never will.
Find out why in volume 2, issue 4 at http://pythonpapers.org: Lance Finn Helsten, "Python Switch Statement"
How to get pythonic switch functionality?
{ key_1: function_1, key_2: function_2, key_3: function_3, key_4: function_4 }.get(x, defaultFunction)()
Advantages of this structure are:
- Any first-class type may be a key: including other functions.
- Access is in constant time (O(1)).
- There is a defaultFunction() to handle invalid keys.
- Late binding of the functions allows for dynamic code.
- The dictionary may be stored after it is created to improve speed (see article): the dictionary is interpreted a single time.
- A dictionary may be selected and then a function generated from that dictionary (see article).
Note: See
TracTickets for help on using
tickets.
