The official tutorial (https://www.python.org/dev/peps/pep-0636/#adding-a-ui-matching-objects) shows that you can use use objects with the new pattern matching syntax, so I wanted to use it with regex matches like this:
import re
match re.match(r"^foo", "foobar"):
case re.Match(span=(start, end), match=match):
print(f"match: {match} ({start}-{end})")
case _:
print("no matches")
But the first case never executes, what am I doing wrong?
re.Matchinstances (re.Match(span=(0,3), match='foo')` Traceback (most recent call last): ` ` File "<stdin>", line 1, in <module> `TypeError: cannot create 're.Match' instances), so they don't have ordinary__init__. Second, they don't define__match_args__explicitly. Unfortunately,reis c-module, so we can't examine direct python equivalent.re.Matchdoesn't define__match_args__which is required for the newmatchstatement.class Car: def __init__(self, a=1): self.a = a. Car instance will match properly.match Car(3): case Car(a=a): print(a)prints 3.Matchobject has nomatchattribute.spanandmatchinre.Matchinstances are methods not attributes for some reason despite what the pretty-printed instance suggests. So I guess it really is not possible to directly use it with match statements without doing extra work.