473,574 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 957
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
2932
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 contents have the same hash code. DateTime date1 = new DateTime(2001,11,22); DateTime date2 = new DateTime(2001,11,22);
1
1618
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 administrator( easiest, but the least recommended) or you can use the identity tag in the web.config file to specify a user whose identity will be...
3
2396
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 with my ASP.NET page and I get an access denied error. I can use the test Windows Form just fine, but not my ASP.NET form. This is all local to my...
5
22331
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 IUSR_<MachineName> pemissions all over the place (registry, files, folders, etc.)! Still can't create the objects with anonymous access enabled. It's not...
1
1990
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 same steps, the auto complete works, the compiler likes my code, but when I test, pressing the button that triggers the code segment gives me an...
2
1159
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 originated in the code. Exception Details: System.UnauthorizedAccessException: Access is denied. ASP.NET is not authorized to access the requested...
11
1319
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 Free.
2
2017
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 like to add a bit of extra information to it such as: User.Identity.FullName User.Identity.Email User.Identity.Organization
11
1895
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 per object, which can be used e.g. to put objects in an associative container (a hash value is not an alternative as it is not fixed during the...
2
4867
by: Yasin Cepeci | last post by:
Server Error in '/' Application. -------------------------------------------------------------------------------- Access to the path 'C:\Inetpub\vhosts\wonderfulreklam.com\subdomains\soganli\httpdocs\memory\resim_yok.jpg' is denied. Description: An unhandled exception occurred during the execution of the current web request. Please review...
0
7797
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8041
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. ...
0
8225
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...
1
7806
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...
0
6440
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...
0
5296
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...
0
3739
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...
0
3748
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2233
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

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.