473,789 Members | 2,898 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1249
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
2653
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 2.2 * 0.46 seconds in Python 2.3
114
9886
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
2869
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 plain english? Thanks, Wayne
9
1627
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
3981
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 resource. If we search PHP, all results are programming resource. Second, python also means snake, snake is not a good thing in western culture. Many people dislike any things relevant to snake. We must have high regard for the custom.
3
1364
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
2708
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 always doing why cant this be automated or abstracted away? Are the instances where I won't pass self? I imagine there is some tradeoff involved otherwise it would have been
2
11713
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 leaking(thoug i konw i just donot have to).Then i look inside in time.h file(mingw) ,and i found notes say"These functions write to and return pointers to static buffers that may be overwritten by other function calls".So how is the"static buffers"...
0
10410
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10200
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10139
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9020
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6769
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5418
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4093
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3701
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2909
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.