473,654 Members | 3,078 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

bizarre id() results

The following was cut and pasted exactly (except for the
# lines which I added after the fact) from an interactive python
session in a Window 2000 cmd.exe window.

Can somebody please explain to me what the heck is
going on?!?!

Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright" , "credits" or "license" for more information.
class A: .... def m1(self): print "m1"
.... def m2(self): print "m2"
.... a = A()
a.m1() m1 a.m2() m2
# ok, both methods work and give the expected results
# so i presume they are different methods. id(a.m1) 9202984 id(a.m2) 9202984 id(a.m1)==id(a. m2) True
# Huh? They seem to be the same. a.m1 is a.m2 False
# But not the same... a.m1 <bound method A.m1 of <__main__.A instance at 0x00923B98>> a.m2 <bound method A.m2 of <__main__.A instance at 0x00923B98>>
# Let's look at them in hex... hex(id(a.m1)) '0x8c6d28' hex(id(a.m2)) '0x8e7b48'
# Now they are different. 0x8c6d28->9202984, 0x8e7b48->9337672 id(a.m1) 9337672 id(a.m2) 9337672
# Now they are both equal to the second one. hex(id(a.m1)) '0x8e7b48' hex(id(a.m2)) '0x8e7b48'
# in hex too. id <built-in function id> hex

<built-in function hex>
# just double checking!

Why??? This is so bizarre I'm sure I am doing something
really stupid.
Dec 15 '05 #1
3 1136
> # ok, both methods work and give the expected results
# so i presume they are different methods.
id(a.m1)
9202984
id(a.m2)
9202984
id(a.m1)==i d(a.m2)


True
# Huh? They seem to be the same.


What you observe is rooted in two things:

- python objects bound methods are created on demand. Consider this
example:
class Foo:
def m(self):
pass

f = Foo()
m1 = f.m
m2 = f.m

print id(m1), id(m2)
The reason is that the method iteself is bound to the instance - this
creates a bound method each time.

The other thing is that this bound method instances are immediaty
garbage collected when you don't keep a reference. That results in the
same address being used for subsequent bound method instantiations:

print id(f.m)
print id(f.m)
results in the same id being printed. It doesn't matter if you use m1,
m2 instead, as in your example.

HTH,

Diez
Dec 15 '05 #2
Stuart McGraw wrote:
The following was cut and pasted exactly (except for the
# lines which I added after the fact) from an interactive python
session in a Window 2000 cmd.exe window.

Can somebody please explain to me what the heck is
going on?!?!

Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright" , "credits" or "license" for more information.
class A: ... def m1(self): print "m1"
... def m2(self): print "m2"
... a = A()
a.m1() m1 a.m2() m2
# ok, both methods work and give the expected results
# so i presume they are different methods. id(a.m1) 9202984 id(a.m2) 9202984 id(a.m1)==id(a. m2) True
# Huh? They seem to be the same. a.m1 is a.m2 False
# But not the same... a.m1 <bound method A.m1 of <__main__.A instance at 0x00923B98>> a.m2 <bound method A.m2 of <__main__.A instance at 0x00923B98>>
# Let's look at them in hex... hex(id(a.m1)) '0x8c6d28' hex(id(a.m2)) '0x8e7b48'
# Now they are different. 0x8c6d28->9202984, 0x8e7b48->9337672 id(a.m1) 9337672 id(a.m2) 9337672
# Now they are both equal to the second one. hex(id(a.m1)) '0x8e7b48' hex(id(a.m2)) '0x8e7b48'
# in hex too. id <built-in function id> hex

<built-in function hex>
# just double checking!

Why??? This is so bizarre I'm sure I am doing something
really stupid.


try running this script:

class bound_simulator :
def __init__(self, method):
self.method = method
print "alloc", method, id(self)
def __del__(self):
print "release", self.method, id(self)

class instance_simula tor:
def __getattr__(sel f, method):
return bound_simulator (method)

i = instance_simula tor()

print id(i.m1)
print id(i.m2)
print id(i.m1) == id(i.m2)
print i.m1 is i.m2

and see if you can figure out what's going on here.

</F>

Dec 15 '05 #3
a.m1 returns a bound method which gets freed before you try checking a.m2,
which ends up getting the same peice of memory. If you save a reference to
the bound methods, they are forced to have separate objects.
class A: .... def m1(self): print "m1"
.... def m2(self): print "m2"
.... a = A()
a.m1() m1 a.m2() m2 id(a.m1) 25402184 id(a.m2) 25402184 b = a.m1
c = a.m2
id(b) 25402184 id(c)
25541848
-Chris

On Thu, Dec 15, 2005 at 02:19:15PM -0700, Stuart McGraw wrote:
The following was cut and pasted exactly (except for the
# lines which I added after the fact) from an interactive python
session in a Window 2000 cmd.exe window.

Can somebody please explain to me what the heck is
going on?!?!

Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)] on win32
Type "help", "copyright" , "credits" or "license" for more information. class A: ... def m1(self): print "m1"
... def m2(self): print "m2"
... a = A()
a.m1() m1 a.m2() m2
# ok, both methods work and give the expected results
# so i presume they are different methods. id(a.m1) 9202984 id(a.m2) 9202984 id(a.m1)==id(a. m2) True
# Huh? They seem to be the same. a.m1 is a.m2 False
# But not the same... a.m1 <bound method A.m1 of <__main__.A instance at 0x00923B98>> a.m2 <bound method A.m2 of <__main__.A instance at 0x00923B98>>
# Let's look at them in hex... hex(id(a.m1)) '0x8c6d28' hex(id(a.m2)) '0x8e7b48'
# Now they are different. 0x8c6d28->9202984, 0x8e7b48->9337672 id(a.m1) 9337672 id(a.m2) 9337672
# Now they are both equal to the second one. hex(id(a.m1)) '0x8e7b48' hex(id(a.m2)) '0x8e7b48'
# in hex too. id <built-in function id> hex

<built-in function hex>
# just double checking!

Why??? This is so bizarre I'm sure I am doing something
really stupid.
--
http://mail.python.org/mailman/listinfo/python-list

Dec 15 '05 #4

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

Similar topics

0
1550
by: Ethan | last post by:
I've been having some really weird problems with a very simple PHP app. I'm wondering if anyone can help me sort this out. I have a page that prints out the results of a MySQL query as an HTML table using a for loop: nothing too exotic there. However, sometimes one column shows the data it's supposed to contain, and sometimes it doesn't. (The name of the field is "category".) I checked the actual DB data. The data is there.
2
1056
by: twbanks | last post by:
I'm doing a select which includes the following: case when (rtrim(ltrim(T464.COMMENT_4)) = '' or T464.COMMENT_4 is null) then '' else convert(datetime,left(replace(replace(T464.COMMENT_4,' QTR: ',''),' - ',''),10)) end as QuarterStartDate
14
2069
by: Michael Carr | last post by:
I have an intermittent problem that occurs on a very small number of browsers that access my website. The strange thing is this: my site works perfectly for 99.9% of browsers. The behavior described below occurs for 0.1% of users, and so far I've only seen it on IE 6.0. My ASP.NET website is set up like this: 1. Logon page - Collect username / password and verify against the DB. If OK, store the UserID in Session and redirect to Main...
1
1290
by: Xeth Waxman | last post by:
Good afternoon, I have a bizarre question. When running the following query: select SomeColumnName from TableA where PK_TableA in (select PK_TableA from TableB) I get results. This should not be feasible, because the query within the in clause:
1
2690
by: zoehart | last post by:
I'm working with VBScript to build a text email message. I'm seeing a variety of bizarre formatting issues. The following lines of code MT = MT & vbCrLf & "Card Type: " & CardType MT = MT & vbCrLf & "Credit Card Number: " & objBOWallet.Encrypt(Payment.) If Not IsNull(Payment.) Then MT = MT & vbCrLf & "Expiration Month: " & Payment. End If If Not IsNull(Payment.) Then MT = MT & vbCrLf & "Expiration Year: " & Payment. End If
4
2306
by: Neo Geshel | last post by:
Just moved to C# from VB.NET, frustrated to hell and back by inability to get much-copied (from about 20+ different resources) literal example to work. Master Page content: <meta name="keywords" content="<asp:Literal ID="ltrlKeywords" Runat="server" />"></meta> Code-Behind:
3
1882
by: Peter | last post by:
Hi! I am having some very strange behavior with my databound controls. It's taken a long time to isolate exactly what is provoking the problem, but I'm still leagues away from solving it. I have a DataView which filters a DataSet. Bound to this dataview is a ListBox, via its DataSource property. The DisplayMember is the name property of the row. Simple enough so far?
1
1017
by: Matt Pegg | last post by:
Hi all, I have a problem where I have a webpage accessing data in an MS Access database, but the results of a: set rs=conn.execute(sql_statement) appear to be cached somewhere.. Its only a recent issue, and the page has been in use for quite a while now without a problem, and I haven't touched the db connection code. Now, however, I get results from the table as they were entered two days ago, not the current data. I've gone...
12
5445
by: Gerhard | last post by:
This is bizarre... Im having problems with the combobox AfterUpdate event: Im running Access 2003. I created an unbound combobox with 3 columns on a form. The Row Source is from a table. Bound Column = 1 and LimitToList = No,,, thus the user can edit the data in the 1st-Column after they selected one of the rows in the combobox. However, I want to save the data of 2nd-Column and 3rd-Column, because if the user edits the 1st-column,...
0
8285
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8706
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
8475
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
7304
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...
1
6160
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5621
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
4149
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
1915
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1592
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.