473,396 Members | 1,860 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,396 software developers and data experts.

Q about object identity

Hello,

I am testing object identity.

If I do it from the interpreter, I get strange results.
>>print [] is []
False
>>print id([]), id([])
3083942700 3083942700

Why is that? Isn't this an error?
If I test it in a script, all is OK.

#!/usr/bin/python

a = []
b = []

print a == b
print a is b

print id(a), id(b)

print id(None), id(None)
print id(True), id(True)
On my computer, I got these results:

True
False
3083769036 3083793516
135553336 135553336
135526444 135526444

All as expected.
jan bodnar
Jun 27 '08 #1
5 954
vr******@gmail.com schrieb:
Hello,

I am testing object identity.

If I do it from the interpreter, I get strange results.
>>>print [] is []
False
>>>print id([]), id([])
3083942700 3083942700

Why is that? Isn't this an error?
No, it's not an error. You are getting this result because the list
implementation keeps a bunch of unused list objects in a free list. It's
an optimization trick. Python has to create two different list objects
for "[] is []" while it can reuse the same list object for id([]) == id([]).

Christian

Jun 27 '08 #2
On 3. Jún, 23:08 h., Christian Heimes <li...@cheimes.dewrote:
vrons...@gmail.com schrieb:
Hello,
I am testing object identity.
If I do it from the interpreter, I get strange results.
>>print [] is []
False
>>print id([]), id([])
3083942700 3083942700
Why is that? Isn't this an error?

No, it's not an error. You are getting this result because the list
implementation keeps a bunch of unused list objects in a free list. It's
an optimization trick. Python has to create two different list objects
for "[] is []" while it can reuse the same list object for id([]) == id([]).

Christian
Aha.

Thanks.

jan bodnar
Jun 27 '08 #3
On Tue, 03 Jun 2008 23:08:46 +0200, Christian Heimes wrote:
vr******@gmail.com schrieb:
>Hello,

I am testing object identity.

If I do it from the interpreter, I get strange results.
>>>>print [] is []
False
>>>>print id([]), id([])
3083942700 3083942700

Why is that? Isn't this an error?

No, it's not an error. You are getting this result because the list
implementation keeps a bunch of unused list objects in a free list. It's
an optimization trick. Python has to create two different list objects
for "[] is []" while it can reuse the same list object for id([]) == id([]).
I don't think you need optimization tricks for the explanation. In ``[]
is []`` there need to be two lists that exist at the same time to be
compared by the ``is`` operator. With ``id([]) == id([])`` just the
id numbers have to exist at the same time, so the execution may look like
this:

• create empty list
• call `id()` with it
• remember first id number
• when `id()` returns, the list is not referenced anymore and can be
garbage collected
• create empty list, and here the list object might get allocated at the
same memory location as the first empty list → same id.
• call `id()` with it
• remember second id number
• garbage collect second empty list
• compare both numbers

Ciao,
Marc 'BlackJack' Rintsch
Jun 27 '08 #4
vr******@gmail.com wrote:
Hello,

I am testing object identity.

If I do it from the interpreter, I get strange results.

>>>>*print [] is []*

*False*

>>>>print id([]), id([])

3083942700 3083942700

Why is that? Isn't this an error?
If I test it in a script, all is OK.

#!/usr/bin/python

a = []
b = []

print a == b
*print a is b*

print id(a), id(b)

print id(None), id(None)
print id(True), id(True)
On my computer, I got these results:

True
*False*
3083769036 3083793516
135553336 135553336
135526444 135526444

All as expected.
jan bodnar
Assuming I'm not missing something obvious here, the results from the
script are the same as those from the interpreter -- the _is_ returns
False in both cases.
Jun 27 '08 #5

On Jun 6, 2008, at 4:27 PM, Ethan Furman wrote:
vr******@gmail.com wrote:
>Hello,
I am testing object identity.
If I do it from the interpreter, I get strange results.
>>>>*print [] is []*
*False*
>>>>print id([]), id([])
3083942700 3083942700
Why is that? Isn't this an error?
in the first statement the two []'s are in memory at the same
time and have different id() signatures. For the second statement
the first id([]) is evaluated and release from memory and then
the second id([]) is evaluated and release from memory. Since
only one of them exist at a time they can have the same id()
signature.

Cheers
Tommy
Jun 27 '08 #6

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

Similar topics

1
by: Clemens Hoffmann | last post by:
Hello, i have a nasty problem with object identity in hash tables. I try to use different objects as keys. It failed bacause i cannot identify object propperly. Different objects with the same...
1
by: SR | last post by:
Hi This is because the ASPNet worker process runs in a user mode who does not have launch permissions for that component. either make the aspnet worker process user identity as an...
3
by: vincentw56 | last post by:
This is the first time I have tried this. What we have is a com object the generates keys. A developer here wrote a .Net wrapper in C# for this com object. I am trying to call her dll wrapper...
5
by: gregfocker | last post by:
OK, the ProgID is correct. The .dll is registered. This is a permission issue. I can create objects when I disable anonymous access on the Web site in IIS (Win XP Pro). And I've given...
1
by: Ben Pryhoda | last post by:
I have already used a third party com object in my code, and it went easily. Add the reference, call the methods, and get my results. But I am integrating a legacy com object and I follow the...
2
by: k | last post by:
Access is denied. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it...
11
by: Amit Agarwal | last post by:
where is the place to attach pricipall object to identity. global file and is it necessary to attach each time user roles to principal object.. amit --- Outgoing mail is certified Virus...
2
by: CodeCowboy | last post by:
I'm sure some of you have done this before and I've been perusing through the forum trying to find some uncomplicated solution. I am trying to extend the existing user.identity object. I would...
11
by: Stephan Keil | last post by:
Hi all, I am a novice with .NET and I am wondering if there is something like an "identity value" of an object. I mean something like the object's address in C++ or C, i.e. a fixed unique value...
2
by: Yasin Cepeci | last post by:
Server Error in '/' Application. -------------------------------------------------------------------------------- Access to the path...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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,...
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...
0
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,...

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.