class Case(object):def __init__(self, value):self.value = valuedef __enter__(self):return selfdef __exit__(self, exc_type, exc_value, traceback):passdef __call__(self, *values):return self.value in valuesdef __eq__(self, value):return self.value == valuedef __lt__(self, value):return self.value < valuedef __gt__(self, value):return self.value > value# etc.# can easily add structural matching or regex matching as wellfor i in range(5):print(i, end=' => ')with Case(i) as case:if case(1):print('one')elif case((2,3)):
print('tuple(two, three)')
elif case(2, 3):print('two or three')elif case > 3:print('more than three')
else:
print('unmatched')
0 => unmatched1 => one2 => two or three3 => two or three4 => more than three