You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
num=5matchnum:
case5:
print('is five')
casedefault:
print('is not five')
They support matching on custom classes too, with the __match_args__ attribute.
classMyClass:
__match_args__= ('num1', 'num2')
def__init__(self, num1, num2):
self.num1=num1self.num2=num2myinstance=MyClass(5, 10)
matchmyinstance:
caseMyClass(5, num2):
print(f'is 5 and {num2}')
caseMyClass(_, num2):
print(f'is not 5 and is {num2}')
I've found myself adding this attribute to pydantic classes by hand, eventhough it seems pretty intuitive to me that the default behavior should match fields in the order that they were specified. So my code looks kind of like this:
Checks
Feature
Python 3.10 introduces the new
match
syntax. https://peps.python.org/pep-0636/Example:
They support matching on custom classes too, with the
__match_args__
attribute.I've found myself adding this attribute to pydantic classes by hand, eventhough it seems pretty intuitive to me that the default behavior should match fields in the order that they were specified. So my code looks kind of like this:
Might as well add it natively?
I'm opening this issue to couple it with a PR :)
The text was updated successfully, but these errors were encountered: