X is confusing

Discussion in 'Python' started by Peter Mott, Jul 6, 2004.

  1. Peter Mott

    Peter Mott Guest

    I get this inb the Python interpreter v. 2.3

    >>>
    >>> X=99
    >>> X is 99

    True
    >>> Y=100
    >>> Y is 100

    False
    >>>


    Numbers less that 100 get True, more than 100 get False. It seems. What does
    this mean? If nothing, then why does it happen? I mean how do you explain
    'is' or 'id(X)' to the first time programmer when it does this to you.

    Peter
    Peter Mott, Jul 6, 2004
    #1
    1. Advertising

  2. On Tue, Jul 06, 2004 at 09:53:35PM +0100, Peter Mott wrote:
    > Numbers less that 100 get True, more than 100 get False. It seems. What does
    > this mean? If nothing, then why does it happen?


    Small integer objects are cached to improve performance. It's an
    implementation detail so don't count on it.

    > I mean how do you explain 'is' or 'id(X)' to the first time
    > programmer when it does this to you.


    Use 'is' as it was intended (i.e. to test object identity).

    Neil
    Neil Schemenauer, Jul 6, 2004
    #2
    1. Advertising

  3. >> I mean how do you explain 'is' or 'id(X)' to the first time
    >> programmer when it does this to you.


    A first time programmer should never need to use 'is', with the exception
    of '... is None' (and even that can be replaced by == as far as a first
    time programmer is concerned).

    An understanding of the meaning of 'is' requires understanding the concept
    of objects, and the difference between object equality and object
    identity. Once those concepts are known, it should be easy to explain
    that since low-valued integers are frequently used, python creates them
    once and reuses them when needed, whereas for larger numbers a new object
    is created every time.
    Paramjit Oberoi, Jul 6, 2004
    #3
  4. Peter Mott

    Duncan Booth Guest

    "Peter Mott" <> wrote in
    news:40eb114e$0$58816$:

    > I get this inb the Python interpreter v. 2.3
    >
    >>>>
    >>>> X=99
    >>>> X is 99

    > True
    >>>> Y=100
    >>>> Y is 100

    > False
    >>>>

    >
    > Numbers less that 100 get True, more than 100 get False. It seems.
    > What does this mean? If nothing, then why does it happen? I mean how
    > do you explain 'is' or 'id(X)' to the first time programmer when it
    > does this to you.
    >


    Similar things happen with strings:

    >>> x = 'hello'
    >>> x is 'hello'

    True
    >>> y = 'hello world'
    >>> y is 'hello world'

    False
    >>>


    The compiler is free to optimise immutable values if it wishes. First time
    programmers probably don't need to know all of the details of 'is', for
    that matter even experienced programmers rarely need to use it.
    Duncan Booth, Jul 7, 2004
    #4
    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. Will

    Confusing design considerations

    Will, Jan 22, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    394
  2. Glenn
    Replies:
    8
    Views:
    391
    Chuck Ritzke
    Apr 24, 2004
  3. Pete Beech
    Replies:
    0
    Views:
    590
    Pete Beech
    Aug 25, 2004
  4. VB Programmer
    Replies:
    1
    Views:
    650
    VB Programmer
    Sep 22, 2004
  5. 2obvious
    Replies:
    2
    Views:
    1,669
    Peter Blum
    Jan 3, 2005
Loading...

Share This Page