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

Gerrit gerrit at nl.linux.org
Tue Feb 10 17:12:50 EST 2004


Dietrich Epp wrote:
> >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).

I don't depend on them, I just want to show them in hex in my __repr__,
so that I can see two objects are equal.

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

Because I want to include more information.

My code really is:

    def __repr__(self):
        s = "<%s sprite(in %d groups) at %x pos=%s>"
        return s % (self.__class__.__name__, len(self.__g), \
                    id(self), self.rect.topleft)

...and the base class happens to define __repr__, which does not include
id(self). I want to be able to see in debugging code that a certain
sprite at pos (x1, y1) at frame N is the same as a certain sprite at
position (x2, y2) in frame M. id(self) seems ideal for that, until these
Warnings started (I now do abs(id(self)), which is ugly enough but gets
rid of the warning).

Any hints on getting this right? Parsing object.__repr__(self) is not
the best solution either ;-)

Gerrit.

-- 
Weather: 4°C  Tuesday 10 February 2004 21:25 Twenthe Netherlands
-- 
PrePEP: Builtin path type
    http://people.nl.linux.org/~gerrit/creaties/path/pep-xxxx.html
Asperger's Syndrome - a personal approach:
	http://people.nl.linux.org/~gerrit/english/




More information about the Python-list mailing list