X is confusing

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Mott

    X is confusing

    I get this inb the Python interpreter v. 2.3
    [color=blue][color=green][color=darkred]
    >>>
    >>> X=99
    >>> X is 99[/color][/color][/color]
    True[color=blue][color=green][color=darkred]
    >>> Y=100
    >>> Y is 100[/color][/color][/color]
    False[color=blue][color=green][color=darkred]
    >>>[/color][/color][/color]

    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


  • Neil Schemenauer

    #2
    Re: X is confusing

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

    Small integer objects are cached to improve performance. It's an
    implementation detail so don't count on it.
    [color=blue]
    > I mean how do you explain 'is' or 'id(X)' to the first time
    > programmer when it does this to you.[/color]

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

    Neil

    Comment

    • Paramjit Oberoi

      #3
      Re: X is confusing

      >> I mean how do you explain 'is' or 'id(X)' to the first time[color=blue][color=green]
      >> programmer when it does this to you.[/color][/color]

      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.

      Comment

      • Duncan Booth

        #4
        Re: X is confusing

        "Peter Mott" <peter@monicol. co.uk> wrote in
        news:40eb114e$0 $58816$bed64819 @news.gradwell. net:
        [color=blue]
        > I get this inb the Python interpreter v. 2.3
        >[color=green][color=darkred]
        >>>>
        >>>> X=99
        >>>> X is 99[/color][/color]
        > True[color=green][color=darkred]
        >>>> Y=100
        >>>> Y is 100[/color][/color]
        > False[color=green][color=darkred]
        >>>>[/color][/color]
        >
        > 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.
        >[/color]

        Similar things happen with strings:
        [color=blue][color=green][color=darkred]
        >>> x = 'hello'
        >>> x is 'hello'[/color][/color][/color]
        True[color=blue][color=green][color=darkred]
        >>> y = 'hello world'
        >>> y is 'hello world'[/color][/color][/color]
        False[color=blue][color=green][color=darkred]
        >>>[/color][/color][/color]

        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.

        Comment

        Working...