Created
February 16, 2023 19:10
-
-
Save L3viathan/c89ecd0d6997d87bb1b0509a106b62e5 to your computer and use it in GitHub Desktop.
custom match-case rules
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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