473,395 Members | 1,348 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 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 1085
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 instances: def initVariables(self): self.e =...
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 variable 'config.modsel' points to a particular...
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. And now I know that this root has NOTHING to do...
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 class named Person with the code below. Each...
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 button. My users find it confusing when they change...
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 thatwill search a "plugins" subdirectory (from where...
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 error. The page just dies. I am using PHP5 with...
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 NETDEV_ALIGN_CONST (NETDEV_ALIGN - 1) ...
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 trivial as updating a visitor counter, I...
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 it not being there when I took it out not knowing...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.