Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Replace all references to one object with references to other

10 views
Skip to first unread message

Jack Bates

unread,
Aug 5, 2011, 3:37:21 PM8/5/11
to pytho...@python.org
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?

Ken Watford

unread,
Aug 5, 2011, 4:25:17 PM8/5/11
to Jack Bates, pytho...@python.org

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.

Emile van Sebille

unread,
Aug 5, 2011, 4:18:05 PM8/5/11
to pytho...@python.org
On 8/5/2011 12:37 PM Jack Bates said...

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

Start with a proxy to your first and have it swap in to the second?

EMile


John Gordon

unread,
Aug 5, 2011, 4:52:15 PM8/5/11
to

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"

John Gordon

unread,
Aug 5, 2011, 4:54:20 PM8/5/11
to
In <j1hl5v$9gj$1...@reader1.panix.com> John Gordon <gor...@panix.com> 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

Steven D'Aprano

unread,
Aug 5, 2011, 8:57:41 PM8/5/11
to
Jack Bates wrote:

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

0 new messages