Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > X is confusing

Reply
Thread Tools

X is confusing

 
 
Peter Mott
Guest
Posts: n/a
 
      07-06-2004
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


 
Reply With Quote
 
 
 
 
Neil Schemenauer
Guest
Posts: n/a
 
      07-06-2004
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
 
Reply With Quote
 
 
 
 
Paramjit Oberoi
Guest
Posts: n/a
 
      07-06-2004
>> 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.
 
Reply With Quote
 
Duncan Booth
Guest
Posts: n/a
 
      07-07-2004
"Peter Mott" <(E-Mail Removed)> wrote in
news:40eb114e$0$58816$(E-Mail Removed):

> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
confusing errors with Mozilla apps WCH Firefox 5 11-29-2005 10:55 PM
Confusing with ntp comp.dcom.sys.cisco Cisco 1 07-25-2005 03:21 PM
Confusing Balloon Message =?Utf-8?B?Q29uZnVzZWQ=?= Wireless Networking 3 06-14-2005 01:20 AM
WAN technologies are confusing.... ike lozada Cisco 3 04-14-2004 03:25 PM
subnets confusing me Charlie Cisco 3 10-22-2003 11:55 AM



Advertisments