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

question about the id()

HI ALL:

Can someone explain why the id() return the same value, and why these values are changing? Thanks you.

Python 2.4.1 (#65, Mar 30 2005, 09:13:57) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
class A(object): .... def f():
.... pass
.... def g():
.... pass
....
a=A()
id(a.f) 11365872 id(a.g) 11365872

class B(object): .... def f():
.... print 1
.... def g():
.... print 3
.... b=B()
id(b.f) 11365872 id(b.g) 11365872 id(a.f), id(a.g), id(b.f), id(b.g) (11365872, 11365872, 11365872, 11365872) a.f is a.g False id(a.f), id(a.g), id(b.f), id(b.g) (11492408, 11492408, 11492408, 11492408) a.f is a.g False id(a.f), id(a.g), id(b.f), id(b.g) (11365872, 11365872, 11365872, 11365872)


Jul 19 '05 #1
1 1237
kyo guan wrote:
Can someone explain why the id() return the same value, and why
these values are changing? Thanks you.

a=A()
id(a.f) 11365872 id(a.g) 11365872
The Python functions f and g, inside of a class A, are
unbound methods. When accessed through an instance what's
returned is a bound method.
A.f <unbound method A.f> A().f <bound method A.f of <__main__.A instance at 0x64198>>
In your code you do a.f, which creates a new bound method.
After the id() call its ref-count goes to zero and its
memory is freed. Next you do a.g which creates a new
bound method. In this case it reuses the same memory location,
which is why you get the same id.

I know Python keeps free lists for some data types. I
suspect bound method objects are tracked this way because
they are made/destroyed so frequently. That would increase
the likelihood of you seeing the same id value.

a.f is a.g False


This is the first time you have two bound methods at
the same time. Previously a bound method was garbage
collected before the next one was created.
id(a.f), id(a.g), id(b.f), id(b.g) (11492408, 11492408, 11492408, 11492408) a.f is a.g False id(a.f), id(a.g), id(b.f), id(b.g) (11365872, 11365872, 11365872, 11365872)


The memory locations changed. Here's a conjecture that
fits the facts and is useful to help understand.

Suppose the free list is maintained as a stack, with
the most recently freed object at the top of the stack,
which is the first to be used for the next object.

The "a.f is a.g" creates two bound methods, one at
11492408 and the other at 11365872. Once the 'is'
is done it dec-refs the two methods, a.f first and
a.g second. In this case the ref counts go to zero
and the memory moved to the free list. At this point
the stack looks like

[11365872, 11492408, ... rest of stack ... ]

You then do a.f. This pulls from the top of the
stack so you get 11365872 again. The id() tells
you that, and then the object gets decrefed and
put back on the stack.
Andrew
da***@dalkescientific.com

Jul 19 '05 #2

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

Similar topics

4
by: Nigel Molesworth | last post by:
I've Googled, but can't find what I need, perhaps I asking the wrong question! I want a "FAQ" page on a web site, I hate those pages that scroll you to the answer so and I figured that a good...
2
by: ben | last post by:
hello, i'm following an algorithm book and am stuck on an early excersise in it, not because of the c programming side of it or even the algorithm side of it, i don't think, but because of maths....
5
by: John | last post by:
Hi, I have an asp.net form where if the user enters an incorect date I change the color of the textbox to red and also display a label field with a message saying the date is invalid. What I...
1
by: Fraggle | last post by:
I have a repeater with controls added at run time. the <template> also contains a <asp:textbox that is made visible on some repeater elements. when I come to read the text info out it has...
10
by: Rider | last post by:
Hi, simple(?) question about asp.net configuration.. I've installed ASP.NET 2.0 QuickStart Sample successfully. But, When I'm first start application the follow message shown. ========= Server...
5
by: Aussie Rules | last post by:
Hi, Having a mental block on this one. Have done it before but can't rack my brain on how... I have an object, with a bunch on property, and I add that object to a combo box. I want the...
6
by: RSH | last post by:
I am still trying to grasp the use of real world Objects and how to conceptualize them using a business scenerio. What I have below is an outline that I am wrestling with trying to figure out a...
0
by: | last post by:
I have a question about spawning and displaying subordinate list controls within a list control. I'm also interested in feedback about the design of my search application. Lots of code is at the...
2
by: robtyketto | last post by:
Greetings, Within my jsp I have HTML code (see below) which accepts input, one of these fields sequence unlike the others is an Integer. <FORM ACTION="wk465682AddFAQ.jsp" METHOD="POST"> Id:...
2
by: Ken Fine | last post by:
I want to add the security question and answer security feature to the ChangePassword control. I am aware that this functionality is built into the PasswordRecovery tool. I have implemented the...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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...
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.