Hi Guido,
On 31/03/2021 6:21 pm, Guido van Rossum wrote:
On Wed, Mar 31, 2021 at 2:30 AM Mark Shannon <m...@hotpy.org
<mailto:m...@hotpy.org>> wrote:
> - Add new `__match_seq__` and `__match_map__` special attributes,
corresponding to new public `Py_TPFLAGS_MATCH_SEQ` and
`Py_TPFLAGS_MATCH_MAP` flags for use in `tp_flags`. When Python
classes are defined with one or both of these attributes set to a
boolean value, `type.__new__` will update the flags on the type to
reflect the change (using a similar mechanism as `__slots__`
definitions). They will be inherited otherwise. For convenience,
`collections.abc.Sequence` will define `__match_seq__ = True`, and
`collections.abc.Mapping` will define `__match_map__ = True`.
>
> Using this in Python would look like:
>
> ```
> class MySeq:
> __match_seq__ = True
> ...
>
> class MyMap:
> __match_map__ = True
> ...
> ```
I don't like the way this need special inheritance rules, where
inheriting one attribute mutates the value of another.
It seems convoluted.
Consider:
class WhatIsIt(MySeq, MyMap):
pass
With __match_container__ it works as expected with no special
inheritance rules.
Wait a minute, do you expect WhatIsIt to be a sequence but not a map?
*I* would expect that it is both, and that's exactly what Brandt's
proposal does. So I see this as a plus.
Earlier you said:
Classes that are both mappings and sequences are ill-conceived.
Let's not compromise semantics or optimizability to support these.
(IOW I agree with Mark here.)
PEP 653 requires that:
(__match_container__ & (MATCH_SEQUENCE | MATCH_MAPPING)) !=
(MATCH_SEQUENCE | MATCH_MAPPING)
Would you require that (__match_seq__ and __match_map__) is always false?
If so, then what is the mechanism for handling the `WhatIsIt` class?
If not, then you loose the ability to make a single test to determine
which patterns can apply.
I think we are close to agreement on the mechanism for selecting which
pattern to match, but I still want the better defined semantics of
PEP 653.
I don't know that PEP 653's semantics are better. Have you analyzed any
*differences* besides the proposal above? I've personally found reading
your pseudo-code very difficult, so I simply don't know.
PEP 653 semantics are more precise. I think that is better :)
Apart from that, I think the semantics are so similar once you've added
__match_seq__/__match_map__ to PEP 634 that is hard to
claim one is better than the other.
My (unfinished) implementation of PEP 653 makes almost no changes to the
test suite.
The code in the examples is Python, not pseudo-code.
That might be easier to follow.
Cheers,
Mark.
--
--Guido van Rossum (python.org/~guido <http://python.org/~guido>)
/Pronouns: he/him //(why is my pronoun here?)/
<http://feministing.com/2015/02/03/how-using-they-as-a-singular-pronoun-can-change-the-world/>
_______________________________________________
Python-Dev mailing list -- python-dev@python.org
To unsubscribe send an email to python-dev-le...@python.org
https://mail.python.org/mailman3/lists/python-dev.python.org/
Message archived at
https://mail.python.org/archives/list/python-dev@python.org/message/TC46W5FKYMKDSWRT77ESVJCSWRHSLCNF/
Code of Conduct: http://python.org/psf/codeofconduct/