Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #86975

Re: Is nan in (nan,) correct?

From Ben Finney <ben+python@benfinney.id.au>
Subject Re: Is nan in (nan,) correct?
Date 2015-03-06 13:55 +1100
References <mailman.67.1425594415.21433.python-list@python.org> <54f90c53$0$12994$c3e8da3$5496439d@news.astraweb.com>
Newsgroups comp.lang.python
Message-ID <mailman.77.1425610573.21433.python-list@python.org> (permalink)

Show all headers | View raw


Steven D'Aprano <steve+comp.lang.python@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

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Is nan in (nan,) correct? random832@fastmail.us - 2015-03-05 17:26 -0500
  Re: Is nan in (nan,) correct? sohcahtoa82@gmail.com - 2015-03-05 15:11 -0800
    Re: Is nan in (nan,) correct? Ben Finney <ben+python@benfinney.id.au> - 2015-03-06 10:20 +1100
      Re: Is nan in (nan,) correct? sohcahtoa82@gmail.com - 2015-03-05 15:27 -0800
        Re: Is nan in (nan,) correct? Ben Finney <ben+python@benfinney.id.au> - 2015-03-06 10:39 +1100
        Re: Is nan in (nan,) correct? Chris Angelico <rosuav@gmail.com> - 2015-03-06 10:40 +1100
    Re: Is nan in (nan,) correct? Chris Angelico <rosuav@gmail.com> - 2015-03-06 10:25 +1100
  Re: Is nan in (nan,) correct? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-06 13:09 +1100
    Re: Is nan in (nan,) correct? Ben Finney <ben+python@benfinney.id.au> - 2015-03-06 13:55 +1100
    Re: Is nan in (nan,) correct? Ethan Furman <ethan@stoneleaf.us> - 2015-03-05 19:18 -0800
    Re: Is nan in (nan,) correct? Ben Finney <ben+python@benfinney.id.au> - 2015-03-06 14:26 +1100
    Re: Is nan in (nan,) correct? Ethan Furman <ethan@stoneleaf.us> - 2015-03-05 19:44 -0800
    Re: Is nan in (nan,) correct? Chris Angelico <rosuav@gmail.com> - 2015-03-06 14:49 +1100
    Re: Is nan in (nan,) correct? random832@fastmail.us - 2015-03-05 23:37 -0500
      Re: Is nan in (nan,) correct? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-07 04:07 +1100
    Re: Is nan in (nan,) correct? Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-03-06 04:46 +0000
  Re: Is nan in (nan,) correct? Rustom Mody <rustompmody@gmail.com> - 2015-03-06 01:50 -0800
    Re: Is nan in (nan,) correct? Chris Angelico <rosuav@gmail.com> - 2015-03-06 21:01 +1100
      Re: Is nan in (nan,) correct? Rustom Mody <rustompmody@gmail.com> - 2015-03-06 02:22 -0800
        Re: Is nan in (nan,) correct? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-07 03:59 +1100
          Re: Is nan in (nan,) correct? Rustom Mody <rustompmody@gmail.com> - 2015-03-06 10:04 -0800
            Re: Is nan in (nan,) correct? Ethan Furman <ethan@stoneleaf.us> - 2015-03-06 10:16 -0800
      Re: Is nan in (nan,) correct? Grant Edwards <invalid@invalid.invalid> - 2015-03-06 15:34 +0000
    Re: Is nan in (nan,) correct? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-07 03:43 +1100
      Re: Is nan in (nan,) correct? Rustom Mody <rustompmody@gmail.com> - 2015-03-06 09:04 -0800
        Re: Is nan in (nan,) correct? Chris Angelico <rosuav@gmail.com> - 2015-03-07 04:16 +1100
          Re: Is nan in (nan,) correct? Rustom Mody <rustompmody@gmail.com> - 2015-03-06 09:36 -0800
        Re: Is nan in (nan,) correct? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-07 10:33 +1100
          Re: Is nan in (nan,) correct? Rustom Mody <rustompmody@gmail.com> - 2015-03-06 18:37 -0800

csiph-web