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:

  1. Any first-class type may be a key: including other functions.
  2. Access is in constant time (O(1)).
  3. There is a defaultFunction() to handle invalid keys.
  4. Late binding of the functions allows for dynamic code.
  5. The dictionary may be stored after it is created to improve speed (see article): the dictionary is interpreted a single time.
  6. A dictionary may be selected and then a function generated from that dictionary (see article).
Note: See TracTickets for help on using tickets.