22.06.20 18:17, nate lust пише:
Matching begins by calling a __match__ (class)method on the type, with
the match target as a parameter. The match method must return an object
that can be evaluated as a bool. If the return value is True, the code
block in this match branch is executed, and execution is passed to
whatever comes after the match syntax. If __match__ returns False,
execution is passed to the next match branch for testing.
If a match branch contains a group of parameters, they are used in the
matching process as well. If __match__ returns True, then the match
target will be tested for the presence of a __unpack__ method. If there
is no such method, the match target is tried as a sequence. If both of
these fail, execution moves on. If there is a __unpack__ method, it is
called and is expected to return a sequence. The length of the sequence
(either the result of __unpack__, or the match target itself) is
compared to the number of supplied arguments. If they match the sequence
is unpacked into variables defined by the arguments and the match is
considered a success and the body is executed. If the length of the
sequence does not match the number of arguments, the match branch fails
and execution continues. This is useful for differentiating tuples of
different lengths, or objects that unpack differently depending on state.
I afraid this is very poor kind of pattern matching, and most users will
need something more powerful.
For tuples:
* If its length is 2, and its first item is GET, and its second item's
type is int, then do A.
* If its first item is GET, and its second item's type is str, then save
the second item and do B (the rest of items are ignored).
* If its first item is PUT, then ignore other items and do C.
For dicts:
* If its "type" key is INT, and it has key "value", then save the value
for "value", and save all other key-values, and do A.
* If its "type" key is STRING, and it has keys "data" and "encoding",
then save the value for "data" and "encoding" and do B.
* If it is an empty dict, then do C.
* If it does not have the "type" key, then do D.
For arbitrary objects:
* If it is an instance of Dog, and its "name" attribute has type str and
starts with "F", and its "color" attribute is contained in
required_colors, and its "legs" attribute > 3, and calling its
"has_tail()" method returns false (not necessary literal False, but
arbitrary false value), then do A.
Oh, and it should work for nested patterns.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at
https://mail.python.org/archives/list/python-ideas@python.org/message/DA2NAYGUUMGTCYVVBGZYUDCXFLACAT4F/
Code of Conduct: http://python.org/psf/codeofconduct/