id(a) == id(b) and a is not b --> bug?

Martin v. Löwis martin at v.loewis.de
Sat Jun 7 13:36:55 EDT 2003


Gerrit Holl <gerrit at nl.linux.org> writes:

> > >>> c = C()
> > >>> c.m is c.m
> > 0
> > >>> id(c.m) == id(c.m)
> > 1
> > 
[...]
> BTW, is there any way I could have found out about this myself, expect
> reading the source (which I can't because of lack of knowledge)?

Most certainly. You could have done

a,b = c.m,c.m
print id(a)
print id(b)

to find out that they are different objects. You could then have done

print id(c.m)
print id(c.m)

to find out that object ids get recycled quickly.

I strongly encourage you to investigate the nature of c.m in more
detail. Find out what the name of the type is, and find out what
fields it has. You might be able to infer for yourself why creating
new objects every time is needed to implement the semantics of
methods.

HTH,
Martin




More information about the Python-list mailing list