Here's a simple class that provides case like syntax. How would modifying the language be better than this? This code makes it clear that all the checks are intended to be against the same value. It can easily be extended to have structural matching or regex matching. I think case/elcase would be confusing as it would be unique to python and it's backwards -- the elcase keywords is used for the normal way that people use case statements while the case keyword implements the broken code that inexperienced C programmers frequently write. With this class, you use if/elif/else exactly the way you usually do, and notice that there is no ambiguity when checking multiple values.
class Case(object): def __init__(self, value): self.value = value
def __enter__(self): return self
def __exit__(self, exc_type, exc_value, traceback): pass
def __call__(self, *values): return self.value in values
def __eq__(self, value): return self.value == value def __lt__(self, value): return self.value < value def __gt__(self, value): return self.value > value # etc. # can easily add structural matching or regex matching as well
for i in range(5): print(i, end=' => ') with Case(i) as case: if case(1): print('one') elif case((2,3)):
elif case(2, 3): print('two or three') elif case > 3: print('more than three')
0 => unmatched 1 => one 2 => two or three 3 => two or three 4 => more than three
Is it worth using a context manager for this? Maybe not. But if not, I think it's even less worthwhile to modify the language to add a case statement.