473,289 Members | 1,729 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,289 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 952
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...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.