Skip to content

Instantly share code, notes, and snippets.

@L3viathan
Created February 16, 2023 19:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save L3viathan/c89ecd0d6997d87bb1b0509a106b62e5 to your computer and use it in GitHub Desktop.
Save L3viathan/c89ecd0d6997d87bb1b0509a106b62e5 to your computer and use it in GitHub Desktop.
custom match-case rules
class MatchMeta(type):
def __instancecheck__(cls, inst):
return cls.isinstance(inst)
def matching(fn):
class cls(metaclass=MatchMeta):
isinstance = fn
return cls
@matching
def even(number):
return isinstance(number, int) and number % 2 == 0
match 22:
case even():
print("even!")
case _:
print("not")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment