On Oct 13, 2019, at 02:38, Steve Jorgensen <ste...@stevej.name> wrote:
> 
> Note that I'm new to this system, so I'm not sure if this will format 
> correctly or whether I'll be able to edit it afterward to format it properly 
> if not. Fingers crossed.
> 
> Examples:
>    import re
>    from collections import Sequence
> 
>    # Equivalent of re.compile(r'b.d').search(<str>)
>    re.compile(r'b.d') in 'abcdef'  # -> <_sre.SRE_Match object; span=(1, 4), 
> match='bcd'>
>    re.compile(r'b.d') in 'xyz'  # -> None
> 
>    # Equivalent of isinstance([1, 2], Sequence)
>    [1, 2] in Sequence  # -> True
> 
>    class BrightColorsMeta(type):
>        def __rin__(self, other):
>            other.startswith('bright ')
> 
>    class BrightColors(metaclass=BrightColorsMeta): pass
> 
>    'red' in BrightColors  # -> False
>    'bright blue' in BrightColors  # -> True

That method already exists, it’s just called `__contains__`. Many types already 
customize this; you even get it for free from mixins like 
`collections.abc.Sequence`.

Also, I’m not sure why you want `BrightColors` to be a class here. It doesn’t 
have any instances, or any other class behavior. It’s sort of like an 
open-ended `Enum`, but without any of the actual enum functionality. But 
anyway, you could write this today if you want it.

I think what you want for the other examples is to add an `__rcontains__` 
method, so instead of `x in y` meaning `y.__contains__(x)`, it means either 
`y.__contains__(x)` or `x.__rcontains__(y)` depending on some rule that you 
have to define. Should it be the same rules as `__radd__`? You probably want to 
work through all of the cases for those rules with some example types.

_______________________________________________
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/LNGP2CIV2B7YJ2GSU5MO4KTQOB3VHU67/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to