Archives Feb. 18, 2016

Implementation of Case Swithch syntax in Python

Python itself has no switch statement, there are 3 kinds of Solutions:

1.use dictionary

cases = {
           case1: do_some_stuff1,
           case2: do_some_stuff2,
           ...
           caseN: do_some_stuffN,
         }
cases.get(var, do_default_stuff)()

2.use lambda

result = {
  'a': lambda x: x * 5,
  'b': lambda x: x + 7,
  'c': lambda x: x - 2
}[value](x)

3.Beck Brian provides a class switch to ...

Continue reading

Previous day

Feb. 13, 2016

Next day

Feb. 21, 2016

Archives