472,353 Members | 1,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

X is confusing

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
Jul 18 '05 #1
3 1058
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
Jul 18 '05 #2
>> 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.
Jul 18 '05 #3
"Peter Mott" <pe***@monicol.co.uk> wrote in
news:40***********************@news.gradwell.net:
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.
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Marc | last post by:
Hi all, I was using Tkinter.IntVar() to store values from a large list of parts that I pulled from a list. This is the code to initialize the...
2
by: David MacQuigg | last post by:
I'm setting up a large hierarchy of module packages and using a variable to select which of many alternative packages to import. For example, the...
1
by: MLH | last post by:
mysql -h host -u user -p Running the above command expects actual values to be substituted for host and user. User I know about. That can be root....
4
by: Mark | last post by:
I'm struggling with how to properly name a specific subset of Properties in my classes. An example will illustrate it best. Let's say you have a...
2
by: Glenn | last post by:
Hi! I've developed an app that displays multiple textbox and combobox fields (most of which are autopostback) on a form along with a submit...
1
by: Justin Richeson via .NET 247 | last post by:
(Type your message here) Ok...wow...this is really confusing, but I think it's similar towhat I'm trying to do. I'm looking to create a program...
2
by: d3vkit | last post by:
Okay so I can NOT get my while loop to work. It's the most confusing thing I've ever come across. It was working fine and then suddenly, nothing. No...
11
by: !truth | last post by:
Hi, i feel confused about the following program, and it's works to get a pointer-member's address. #define NETDEV_ALIGN 32 #define...
11
by: James R. Davis | last post by:
Yes, a newbie here. Though I am making progress, slowly, I am also getting more and more confused. With ASP, when I wanted to do something as...
7
by: Andy B | last post by:
I saw this in the set accessor of a property: Set(ByVal value As DataSet) What exactly does the stuff in the () mean? VS complained about...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.