Switch statement in Python

#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()]()

Published: by ;

Table of Contents

Current Ref:

  • til/programming/python/python-switch-statement.md