472,378 Members | 1,456 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Python "is" behavior

I am not certain why this is the case, but...
>>a = 256
b = 256
a is b
True
>>a = 257
b = 257
a is b
False

Can anyone explain this further? Why does it happen? 8-bit integer
differences?
Jun 27 '08 #1
4 1184
On Jun 20, 12:31 pm, michalis.avr...@gmail.com wrote:
I am not certain why this is the case, but...
>a = 256
b = 256
a is b

True
>a = 257
b = 257
a is b

False

Can anyone explain this further? Why does it happen? 8-bit integer
differences?
No, implementation-dependent optimization (caching). For all we know,
the next python version may cache up to 1024 or it may turn off
caching completely; do not rely on it. More generally, do not use 'is'
when you really mean '=='.

George
Jun 27 '08 #2
On Jun 20, 9:42*am, George Sakkis <george.sak...@gmail.comwrote:
On Jun 20, 12:31 pm, michalis.avr...@gmail.com wrote:
I am not certain why this is the case, but...
>>a = 256
>>b = 256
>>a is b
True
>>a = 257
>>b = 257
>>a is b
False
Can anyone explain this further? Why does it happen? 8-bit integer
differences?

No, implementation-dependent optimization (caching). For all we know,
the next python version may cache up to 1024 or it may turn off
caching completely; do not rely on it. More generally, do not use 'is'
when you really mean '=='.

George
Thank you George. I am very curious about some of these internal
Python things that I keep stumbling upon through friends. And thank
you for all the help!
Jun 27 '08 #3
On Jun 20, 12:45 pm, michalis.avr...@gmail.com wrote:
On Jun 20, 9:42 am, George Sakkis <george.sak...@gmail.comwrote:
On Jun 20, 12:31 pm, michalis.avr...@gmail.com wrote:
I am not certain why this is the case, but...
>a = 256
>b = 256
>a is b
True
>a = 257
>b = 257
>a is b
False
Can anyone explain this further? Why does it happen? 8-bit integer
differences?
No, implementation-dependent optimization (caching). For all we know,
the next python version may cache up to 1024 or it may turn off
caching completely; do not rely on it. More generally, do not use 'is'
when you really mean '=='.
George

Thank you George. I am very curious about some of these internal
Python things that I keep stumbling upon through friends. And thank
you for all the help!
As far it's plain curiosity it's ok, but it's a small implementation
detail you shouldn't rely on. There's nothing magic about 256, just
the size decided for 2.5. If you tried it on 2.4 you'd get:

Python 2.4.2 (#1, Mar 8 2006, 13:24:00)
[GCC 3.4.4 20050721 (Red Hat 3.4.4-2)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>a=99
b=99
a is b
True
>>a=100
b=100
a is b
False

I was more surprised by the following:

Python 2.5.1 (r251:54863, May 8 2007, 14:46:30)
[GCC 3.4.6 20060404 (Red Hat 3.4.6-3)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>a= 123456; b=123456; a is b
True

For some reason, stacking multiple statements reuses the same object.

George
Jun 27 '08 #4
For some reason, stacking multiple statements reuses the same object.

Each code object has a co_consts tuple referring to all constants used
in the code. The compiler interns duplicate constants for a single
compiler run, resulting in the same object being used when the code
is put into a single line (or in a function, or module).

When the code is split into multiple interactive statements, the
compiler runs multiple times, and doesn't know anything about past
runs.

Regards,
Martin
Jun 27 '08 #5

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

Similar topics

36
by: Armin Rigo | last post by:
Hi! This is a rant against the optimization trend of the Python interpreter. Sorting a list of 100000 integers in random order takes: * 0.75 seconds in Python 2.1 * 0.51 seconds in Python...
114
by: Maurice LING | last post by:
This may be a dumb thing to ask, but besides the penalty for dynamic typing, is there any other real reasons that Python is slower than Java? maurice
20
by: Wayne Sutton | last post by:
OK, I'm a newbie... I'm trying to learn Python & have had fun with it so far. But I'm having trouble following the many code examples with the object "self." Can someone explain this usage in...
9
by: Lonnie Princehouse | last post by:
There doesn't seem to be any way to customize the behavior of "is" as can be done for other operators... why not?
92
by: ureuffyrtu955 | last post by:
Python is a good programming language, but "Python" is not a good name. First, python also means snake, Monty Python. If we search "python" in google, emule, many results are not programming...
3
by: Jean-Paul Calderone | last post by:
On Fri, 20 Jun 2008 09:31:57 -0700 (PDT), michalis.avraam@gmail.com wrote: http://mail.python.org/pipermail/python-list/2001-November/113994.html Jean-Paul
8
by: ssecorp | last post by:
I first learned about OO from Java. I much prefer to program in Python though. However I am consufed about 2 things. 1. Why do I have to pass self into every method in a class? Since I am...
2
by: chenxinleo | last post by:
Hi, When i use some standard library functions and fields,which return char* type(like ctime in time.h, optarg in getopt.h)and do not have to be freed after calling,i always worry about memory...
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 required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
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 synthesis of my design into a bitstream, not the C++...
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. Background colors can be used to highlight important...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
1
by: ezappsrUS | last post by:
Hi, I wonder if someone knows where I am going wrong below. I have a continuous form and two labels where only one would be visible depending on the checkbox being checked or not. Below is the...
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...
0
by: F22F35 | last post by:
I am a newbie to Access (most programming for that matter). I need help in creating an Access database that keeps the history of each user in a database. For example, a user might have lesson 1 sent...

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.