A context manager-based case statement
I wanted to have a post with some code in, for testing purposes, so here is a little
something I put together based on this Programmers.SE question. Switch
(ab?)uses
Python's context manager with
statement syntax to implement a rough approximation
of the switch
available in some other languages.
Also available as a Gist.
:::python
class Switch(object):
"""A class for faking switch syntax with a context manager.
Args:
value (object): The stored value to compare any cases to.
Example:
>>> with Switch(1) as case:
... if case(1):
... print('unity')
...
unity
>>> with Switch(3) as case:
... if case(1):
... print('unity')
... elif case(2, 3, 4):
... print('small')
...
small
>>> with Switch(5) as case:
... if case(1):
... print('unity')
... elif case(2, 3, 4):
... print('small')
... else:
... print('more than four')
...
more than four
"""
def __init__(self, value):
"""Create a new Switch instance."""
self.value = value
def __call__(self, *cases):
"""Do any of the supplied cases match the stored value?"""
return any(case == self.value for case in cases)
def __enter__(self):
"""Enter the context manager."""
return self
def __exit__(self, typ, value, traceback):
"""Don't do anything when leaving the context manager."""
pass
Comments !