Switch statement in Python

Love switch statement? We can design a switch statement it in python.

We simply create a dispatch for the cases.

Here is the example from the references.

import datetime

dispatch = {
  0: do_monday,
  1: do_tuesday,
  2: do_wednesday,
  3: do_thursday,
  4: do_friday,
  5: do_saturday,
  6: do_sunday
}

my_special_day = datetime.date(2019, 5, 25)

dispatch[my_special_day.weekday()]()

Planted: by ;

L Ma (2019). 'Switch statement in Python', Datumorphism, 08 April. Available at: https://datumorphism.leima.is/til/programming/python/python-switch-statement/.