hex(id(...)) and negative id's, 6 questions

Dietrich Epp dietrich at zdome.net
Sun Feb 8 15:50:29 EST 2004


On Feb 7, 2004, at 2:37 AM, Gerrit wrote:

> Hi,
>
> in my own classes, I usually implement __repr__ with something like:
>
>             hex(id(obj))
>
> ...in order to simulate the behaviour of built-in types.
>
> However, since I upgraded to Fedora Core 1 and Linux Kernel
> 2.4.22-1.2163.nptl, id(obj) is usually negative. Why is it negative?

My guess is that it's due to changes in memory allocation.  It's 
nothing to worry about.  Don't depend on the values returned by id(), 
they depend on the OS, the version of Python, the type of the object, 
and the phases of the moons of Saturn.  For instance, since I use OS X, 
my id()s are very small (and they fit in short-term memory).

Obj uses %p in it's __repr__ function, which is implemented in C, not 
Python.  It doesn't even call id().

My question is why you implement __repr__, when you get object's 
behavior for free.

 >>> class foo:
...     pass
...
 >>> class bar(object):
...     pass
...
 >>> print repr(foo()), repr(bar())
<__main__.foo instance at 0x6d260> <__main__.bar object at 0x6b730>

All automatic!





More information about the Python-list mailing list