Steven D'Aprano <steve+comp.lang.pyt...@pearwood.info> writes: > Since reflexivity is *almost* universal, and using object identity > permits very substantial optimizations, the core developers agreed > that built-in contain types may assume that `x is y` implies `x == y`. > Users of NANs and other non-reflexive types can subclass or define > their own membership function.
On a type (such as a hypothetical SQL NULL type) which does not have reflexivity – i.e. that ‘(x is x) == (x == x)’ may be False – which method needs to be implemented so items *containing* values of that type will have the expected semantics? I can only think of ‘footype.__contains__’, but that's a method of the *container* type, and the ‘in’ operator doesn't consult that method of the items themselves. So, given the hypothetical NullType:: class NullType(object): """ A type whose value never equals any other. This type's values will behave correctly when tested for membership in a collection:: >>> foo = NullType() >>> bar = NullType() >>> foo is foo True >>> foo is bar False >>> foo == foo False >>> foo == bar False >>> quux = [foo, "spam"] >>> "spam" in quux True >>> foo in quux True >>> bar in quux False """ def __eq__(self, value): return False def __method_which_the_in_operator_interrogates__(self, collection): """ Method which the ‘is’ operator interrogates for membership. """ return is_a_member_of(container, self) What method of NullType replaces the hypothetical ‘__method_which_the_in_operator_interrogates__’, which I've implemented to as you describe “define their own membership function”, in order to get the correct behaviour in the doctest above? -- \ “Why am I an atheist? I ask you: Why is anybody not an atheist? | `\ Everyone starts out being an atheist.” —Andy Rooney, _Boston | _o__) Globe_ 1982-05-30 | Ben Finney -- https://mail.python.org/mailman/listinfo/python-list