Re: <var> is None vs. <var> == None

Discussion in 'Python' started by Steve Holden, Jan 23, 2009.

  1. Steve Holden

    Steve Holden Guest

    Chris Rebert wrote:
    > On Fri, Jan 23, 2009 at 11:58 AM, Gerald Britton
    > <> wrote:
    >> Hi -- Some time ago I ran across a comment recommending using <var> is
    >> None instead of <var> == None (also <var> is not None, etc.) My own
    >> testing indicates that the former beats the latter by about 30% on
    >> average. Not a log for a single instruction but it can add up in
    >> large projects.
    >>
    >> I'm looking for a (semi)-official statement on this, but couldn't find
    >> one with normal googling. Can someone please send a link?

    >
    >>From http://www.python.org/dev/peps/pep-0008/ ,

    > 2nd bullet under "Programming Recommendations":
    >
    > - Comparisons to singletons like None should always be done with
    > 'is' or 'is not', never the equality operators.
    >
    >
    > Just FYI, `is` compares object identity (pointer equality) whereas ==
    > checks for value equivalence (which causes a method call). This is why
    > it's significantly faster.
    > But the main reason is because it's idiomatic.
    >

    And, just for completeness, the "is" test is canonical precisely because
    the interpreter guarantees there is only ever one object of type None,
    so an identity test is always appropriate. Even the copy module doesn't
    create copies ...

    >>> import copy
    >>> n = copy.copy(None)
    >>> id(n)

    1555754480
    >>> id(None)

    1555754480
    >>>


    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
     
    Steve Holden, Jan 23, 2009
    #1
    1. Advertising

  2. Steve Holden

    Roger Guest

    > And, just for completeness, the "is" test is canonical precisely because
    > the interpreter guarantees there is only ever one object of type None,
    > so an identity test is always appropriate. Even the copy module doesn't
    > create copies ...
    >


    Does the interpreter guarantee the same for False and True bools?
     
    Roger, Jan 23, 2009
    #2
    1. Advertising

  3. On Jan 23, 12:48 pm, Roger <> wrote:
    > > And, just for completeness, the "is" test is canonical precisely because
    > > the interpreter guarantees there is only ever one object of type None,
    > > so an identity test is always appropriate. Even the copy module doesn't
    > > create copies ...

    >
    > Does the interpreter guarantee the same for False and True bools?


    Yes. I know that there are the PyObject* structs defined for you
    Py_True, Py_False and Py_None in the C level. Confusingly enough, the
    integers -5 through 257 are also singletons where the is test will
    work, but any int out of that range will not.

    >>> def copy_id(x):

    .... id1, id2 = id(x), id(copy.deepcopy(x))
    .... print "Equal: %x %s %x" % (id1, ('==' if id1 == id2 else '!
    ='), id2)
    ....
    ....
    >>> copy_id(a())

    Equal: a8fc90 != f32370
    >>> copy_id(1)

    Equal: 9559c8 == 9559c8
    >>> copy_id(None)

    Equal: 1e1da9f0 == 1e1da9f0
    >>> copy_id(True)

    Equal: 1e1c5ec4 == 1e1c5ec4
    >>> copy_id(False)

    Equal: 1e1c5eb8 == 1e1c5eb8
    >>> copy_id("Hello")

    Equal: 1058840 == 1058840
    >>> copy_id([])

    Equal: 1067030 != 10673f0

    .... large equal integers are not identical in memory location ...

    In [34]: x = 19591
    In [35]: y = 19590+1
    In [36]: id(x), id(y)
    Out[36]: (17937008, 17936588)

    .... but small ones are ...

    In [40]: x = 2
    In [41]: y = 1+1
    In [42]: id(x), id(y)
    Out[42]: (9787836, 9787836)
     
    Jason Scheirer, Jan 23, 2009
    #3
  4. Steve Holden

    John Machin Guest

    On Jan 24, 7:48 am, Roger <> wrote:
    > > And, just for completeness, the "is" test is canonical precisely because
    > > the interpreter guarantees there is only ever one object of type None,
    > > so an identity test is always appropriate. Even the copy module doesn't
    > > create copies ...

    >
    > Does the interpreter guarantee the same for False and True bools?


    Why do you think it matters? Consider the circumstances under which
    you would use each of the following:
    if some_name == True:
    if some_name is True:
    if some_name:
     
    John Machin, Jan 23, 2009
    #4
  5. Steve Holden

    Roger Guest

    > Why do you think it matters?

    Intellectual curiosity hence why I asked the question. It doesn't
    matter if I know why the sky is blue but it's interesting to know
    regardless.
     
    Roger, Jan 23, 2009
    #5
  6. Steve Holden

    Terry Reedy Guest

    Roger wrote:
    >> And, just for completeness, the "is" test is canonical precisely because
    >> the interpreter guarantees there is only ever one object of type None,
    >> so an identity test is always appropriate. Even the copy module doesn't
    >> create copies ...
    >>

    >
    > Does the interpreter guarantee the same for False and True bools?


    Yes. Bool(x) should return one of the two existing instances.

    In 2.x, the *names* 'True' and 'False' can be rebound because bool is
    new and people write
    try:
    False,True
    except NameError:
    False,True = 0,1

    to make code back compatible.

    In 3.0, the names are keywords, just like 'None' and cannot be rebound,
    so x is True is guaranteed to answer whether x *is* the true object.

    Back before rebinding 'None' was prohibited, 'is None' was not
    completely guaranteed either (absent reading the rest of a file to be
    sure no rebinding would be done).
     
    Terry Reedy, Jan 26, 2009
    #6
  7. Steve Holden

    Steve Holden Guest

    Terry Reedy wrote:
    > Roger wrote:
    >>> And, just for completeness, the "is" test is canonical precisely because
    >>> the interpreter guarantees there is only ever one object of type None,
    >>> so an identity test is always appropriate. Even the copy module doesn't
    >>> create copies ...
    >>>

    >>
    >> Does the interpreter guarantee the same for False and True bools?

    >
    > Yes. Bool(x) should return one of the two existing instances.
    >
    > In 2.x, the *names* 'True' and 'False' can be rebound because bool is
    > new and people write
    > try:
    > False,True
    > except NameError:
    > False,True = 0,1
    >
    > to make code back compatible.
    >

    I would claim that the ability to rebind True and False is a simple bug,
    though one not likely to be fixed in an 2.x release. The code above
    doesn't rebind True and False in interpreters that have them ...

    > In 3.0, the names are keywords, just like 'None' and cannot be rebound,
    > so x is True is guaranteed to answer whether x *is* the true object.
    >
    > Back before rebinding 'None' was prohibited, 'is None' was not
    > completely guaranteed either (absent reading the rest of a file to be
    > sure no rebinding would be done).
    >

    And that was a bug too, in this case one that *was* removed in 2.4, I
    believe. Don't have 2.3 lying around just now.

    Python 2.4.3 (#1, May 24 2008, 13:47:28)
    [GCC 4.1.2 20070626 (Red Hat 4.1.2-14)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> None = 3

    SyntaxError: assignment to None

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
     
    Steve Holden, Jan 26, 2009
    #7
  8. Steve Holden

    Terry Reedy Guest

    Steve Holden wrote:
    > Terry Reedy wrote:


    >> In 2.x, the *names* 'True' and 'False' can be rebound because bool is
    >> new and people write
    >> try:
    >> False,True
    >> except NameError:
    >> False,True = 0,1
    >>
    >> to make code back compatible.
    >>

    > I would claim that the ability to rebind True and False is a simple bug,
    > though one not likely to be fixed in an 2.x release. The code above
    > doesn't rebind True and False in interpreters that have them ...


    In pre-bool 2.x, people never wrote the above but sometime wrote
    False,True = 0,1

    To me it is hardly a bug to not gratuitously break substantial amounts
    of proper code.

    >> Back before rebinding 'None' was prohibited, 'is None' was not
    >> completely guaranteed either (absent reading the rest of a file to be
    >> sure no rebinding would be done).
    >>

    > And that was a bug too, in this case one that *was* removed in 2.4, I
    > believe. Don't have 2.3 lying around just now.


    Unlike with True and False, the devs could not think of or find any
    proper use case for rebinding None and judged that the new prohibition
    would break very little if any code. As far as I know, they were correct.

    tjr
     
    Terry Reedy, Jan 26, 2009
    #8
  9. Steve Holden

    Steve Holden Guest

    Terry Reedy wrote:
    > Steve Holden wrote:
    >> Terry Reedy wrote:

    >
    >>> In 2.x, the *names* 'True' and 'False' can be rebound because bool is
    >>> new and people write
    >>> try:
    >>> False,True
    >>> except NameError:
    >>> False,True = 0,1
    >>>
    >>> to make code back compatible.
    >>>

    >> I would claim that the ability to rebind True and False is a simple bug,
    >> though one not likely to be fixed in an 2.x release. The code above
    >> doesn't rebind True and False in interpreters that have them ...

    >
    > In pre-bool 2.x, people never wrote the above but sometime wrote
    > False,True = 0,1
    >

    Right. This is the use case I overlooked.

    > To me it is hardly a bug to not gratuitously break substantial amounts
    > of proper code.
    >

    I quite agree. I take it all back!

    >>> Back before rebinding 'None' was prohibited, 'is None' was not
    >>> completely guaranteed either (absent reading the rest of a file to be
    >>> sure no rebinding would be done).
    >>>

    >> And that was a bug too, in this case one that *was* removed in 2.4, I
    >> believe. Don't have 2.3 lying around just now.

    >
    > Unlike with True and False, the devs could not think of or find any
    > proper use case for rebinding None and judged that the new prohibition
    > would break very little if any code. As far as I know, they were correct.
    >

    Indeed they were.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
     
    Steve Holden, Jan 26, 2009
    #9
  10. Steve Holden

    Steve Holden Guest

    Terry Reedy wrote:
    > Steve Holden wrote:
    >> Terry Reedy wrote:

    >
    >>> In 2.x, the *names* 'True' and 'False' can be rebound because bool is
    >>> new and people write
    >>> try:
    >>> False,True
    >>> except NameError:
    >>> False,True = 0,1
    >>>
    >>> to make code back compatible.
    >>>

    >> I would claim that the ability to rebind True and False is a simple bug,
    >> though one not likely to be fixed in an 2.x release. The code above
    >> doesn't rebind True and False in interpreters that have them ...

    >
    > In pre-bool 2.x, people never wrote the above but sometime wrote
    > False,True = 0,1
    >

    Right. This is the use case I overlooked.

    > To me it is hardly a bug to not gratuitously break substantial amounts
    > of proper code.
    >

    I quite agree. I take it all back!

    >>> Back before rebinding 'None' was prohibited, 'is None' was not
    >>> completely guaranteed either (absent reading the rest of a file to be
    >>> sure no rebinding would be done).
    >>>

    >> And that was a bug too, in this case one that *was* removed in 2.4, I
    >> believe. Don't have 2.3 lying around just now.

    >
    > Unlike with True and False, the devs could not think of or find any
    > proper use case for rebinding None and judged that the new prohibition
    > would break very little if any code. As far as I know, they were correct.
    >

    Indeed they were.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
     
    Steve Holden, Jan 26, 2009
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gerald Britton

    <var> is None vs. <var> == None

    Gerald Britton, Jan 23, 2009, in forum: Python
    Replies:
    7
    Views:
    320
    Steve Holden
    Jan 24, 2009
  2. length power
    Replies:
    2
    Views:
    80
    Rustom Mody
    Apr 10, 2014
  3. Skip Montanaro
    Replies:
    0
    Views:
    62
    Skip Montanaro
    Apr 10, 2014
  4. Johannes Schneider

    Re: why i have the output of [None, None, None]

    Johannes Schneider, Apr 10, 2014, in forum: Python
    Replies:
    0
    Views:
    53
    Johannes Schneider
    Apr 10, 2014
  5. Terry Reedy
    Replies:
    0
    Views:
    63
    Terry Reedy
    Apr 10, 2014
Loading...

Share This Page