Replace all references to one object with references to other
steve+comp.lang.python at pearwood.info
Fri Aug 5 20:57:41 EDT 2011
Jack Bates 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
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
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
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.
More information about the Python-list