Skip to content

Instantly share code, notes, and snippets.

@sug0
Created February 11, 2021 23:51
Show Gist options
  • Select an option

  • Save sug0/c69ec7ef37c300f725573e4141a5c486 to your computer and use it in GitHub Desktop.

Select an option

Save sug0/c69ec7ef37c300f725573e4141a5c486 to your computer and use it in GitHub Desktop.
PEP pattern matching is useless
def match(value, clauses):
for m in clauses:
try:
exec(f'{m} = {value}')
return clauses[m](locals())
except (TypeError, ValueError):
continue
raise ValueError('non exhaustive patterns')
def main():
value = match((5, 1, 3), {
'(x, y)': lambda env: env['x'],
'(x, y, z)': lambda env: env['z'],
})
print(value)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment