[C++-sig] Boost Python objects and object identity

Pertti Kellomäki pertti.kellomaki at tut.fi
Tue Sep 29 20:29:47 CEST 2009

I have Python bindings for a C++ library that among other
things contains classes for describing the static structure
of a microprocessor.

My background is in Lisp, so I am used to using object
identities (references) as keys. The C++ library can
be used in this fashion, e.g. looking up a register file
by name always returns the same object reference.
However, the C++ to Python mapping does not preserve object
identity, as a new Python object is created for every query,
even if the underlying C++ reference is the same.

Is there any easy way to preserve object identity in this
sense with Boost Python? My first thought was to use some
kind of caching scheme so that consequent queries with a
particular name return the same object, but the problem is
that objects are not only looked up by name, they can also
be returned e.g. by asking for a parent object in a containment

More information about the Cplusplus-sig mailing list