Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Replace all references to one object with references to other

Reply
Thread Tools

Replace all references to one object with references to other

 
 
Jack Bates
Guest
Posts: n/a
 
      08-05-2011
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?
 
Reply With Quote
 
 
 
 
John Gordon
Guest
Posts: n/a
 
      08-05-2011
In <(E-Mail Removed)> Jack Bates <(E-Mail Removed)> 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
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
http://www.velocityreviews.com/forums/(E-Mail Removed) B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
 
 
 
John Gordon
Guest
Posts: n/a
 
      08-05-2011
In <j1hl5v$9gj$(E-Mail Removed)> John Gordon <(E-Mail Removed)> writes:

> In <(E-Mail Removed)> Jack Bates <(E-Mail Removed)> 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

--
John Gordon A is for Amy, who fell down the stairs
(E-Mail Removed) B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
Steven D'Aprano
Guest
Posts: n/a
 
      08-06-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
> try?


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Storing const references to other objects in another object. James Allsopp C++ 11 04-09-2010 12:36 PM
How to design an object to get references to other needed objects? itaj sherman C++ 1 02-25-2009 06:25 PM
Help getting rid of All-in-one device and replace with CISCO product(s) atcochran@gmail.com Cisco 1 09-23-2006 03:08 AM
How to replace one JButton by two other JButtons during application run ? Ulf Meinhardt Java 1 11-14-2005 05:23 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM



Advertisments