__contains__ classmethod?
Random832
random832 at fastmail.com
Mon Dec 18 16:55:10 EST 2017
On Mon, Dec 18, 2017, at 16:25, Tim Chase wrote:
> My understanding was that "in" makes use of an available __contains__
> but something seems to preventing Python from finding that.
>
> What's going on here?
Most __ methods have to be an actual method on the class object, which
means you have to use a metaclass.
"one" in X() works, but to get "one" in X to work, you need:
class M(type):
def __contains__(cls, v):
return v in cls._ALL
class X(object, metaclass=M):
ONE = "one"
TWO = "two"
_ALL = frozenset(v for k,v in locals().items() if k.isupper())
If you want *both* "one" in X and "one" in X() to work, I think you
pretty much have to define it in both places.
More information about the Python-list
mailing list