Replace all references to one object with references to other

Ken Watford kwatford at
Fri Aug 5 16:25:17 EDT 2011

On Fri, Aug 5, 2011 at 3:37 PM, Jack Bates <ms419 at> wrote:
> I have two objects, and I want to replace all references to the first
> object - everywhere - with references to the second object. What can I
> try?

If using PyPy instead of CPython is an option, the "thunk" object
space's "become" function can apparently do this:

In CPython, this might be a tad difficult. At the C level, a reference
to a python object is just a pointer to it. You could iterate through
the entire address space looking for values that equal a particular
pointer, but changing them would be dangerous, since memory isn't
labeled by type - you can't tell if the memory is a pointer to your
object or an important part of some other data structure.

If you could narrow down what you want to accomplish, this might be
easier. For instance, if all you need is to replace module-level
references to the object, that can be done.

More information about the Python-list mailing list