If using PyPy instead of CPython is an option, the "thunk" object
space's "become" function can apparently do this:
http://doc.pypy.org/en/latest/objspace-proxies.html#the-thunk-object-space
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.
Start with a proxy to your first and have it swap in to the second?
EMile
The simplest answer to your question is to assign object2 to object1
at the very beginning of your code, but that is a very naive solution
and can easily fail based on lots of factors.
What's your context: A single source file? Many source files? A live
application with persistent data?
What are your two objects? Do they provide an identical interface?
--
John Gordon A is for Amy, who fell down the stairs
gor...@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"
> In <mailman.1937.1312573...@python.org> Jack Bates <ms...@freezone.co.uk> writes:
> > 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?
> The simplest answer to your question is to assign object2 to object1
I think I have that backwards, but the intent should be clear:
object1 = object2
# many references to object1 follow, which will now reference object2
Another way of solving your *actual* problem.
"Replace all references to object1 with object2 instead" is a means to an
end, not the end itself. What are you trying to solve? Focus on *that*
problem, not your supposed solution, because "replace all..." is doomed to
fail.
There is no "master list" of objects available to you. All you have is one
or more namespaces containing objects. Many of those objects themselves
will contain other objects, and so on. All you can do is walk through each
namespace in turn, recursively into each object, searching for the object
you want to replace. But that may not help you, because there's no
guarantee that having found it you can replace it safely, *or at all*.
While Python does allow code to reach deeply into the internals of a wide
range of objects -- very little is truly private in Python -- do you
*really* want to be taking responsibility for safely replacing objects from
within arbitrary other objects? If so, Python gives you the tools to shoot
yourself in the foot, although it won't necessarily be easy, or pretty, or
fast.
So, tell us what your real problem is, the end towards which you
think "replace all..." is the solution, and we'll see if we can help.
--
Steven