bizarre id() results

S

Stuart McGraw

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..... def m1(self): print "m1"
.... def m2(self): print "m2"
....m2
# ok, both methods work and give the expected results
# so i presume they are different methods.True
# Huh? They seem to be the same. False
# But not the same...'0x8e7b48'
# Now they are different. 0x8c6d28->9202984, 0x8e7b48->93376729337672
# Now they are both equal to the second one.'0x8e7b48'
# in hex too.<built-in function hex>
# just double checking!

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

Fredrik Lundh

Stuart said:
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.... def m1(self): print "m1"
... def m2(self): print "m2"
...m2
# ok, both methods work and give the expected results
# so i presume they are different methods.True
# Huh? They seem to be the same.False
# But not the same...'0x8e7b48'
# Now they are different. 0x8c6d28->9202984, 0x8e7b48->93376729337672
# Now they are both equal to the second one.'0x8e7b48'
# in hex too.<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_simulator:
def __getattr__(self, method):
return bound_simulator(method)

i = instance_simulator()

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>
 
D

Diez B. Roggisch

# ok, both methods work and give the expected results
# so i presume they are different methods.


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
 
C

Chris Lambacher

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.
.... def m1(self): print "m1"
.... def m2(self): print "m2"
.... 25541848


-Chris

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.... def m1(self): print "m1"
... def m2(self): print "m2"
...m2
# ok, both methods work and give the expected results
# so i presume they are different methods.True
# Huh? They seem to be the same. False
# But not the same...'0x8e7b48'
# Now they are different. 0x8c6d28->9202984, 0x8e7b48->93376729337672
# Now they are both equal to the second one.'0x8e7b48'
# in hex too.<built-in function hex>
# just double checking!

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top