Replace all references to one object with references to other

Discussion in 'Python' started by Jack Bates, Aug 5, 2011.

  1. Jack Bates

    Jack Bates Guest

    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?
    Jack Bates, Aug 5, 2011
    #1
    1. Advertising

  2. Jack Bates

    John Gordon Guest

    In <> Jack Bates <> 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
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
    John Gordon, Aug 5, 2011
    #2
    1. Advertising

  3. Jack Bates

    John Gordon Guest

    In <j1hl5v$9gj$> John Gordon <> writes:

    > In <> Jack Bates <> 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
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
    John Gordon, Aug 5, 2011
    #3
  4. 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
    Steven D'Aprano, Aug 6, 2011
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ulf Meinhardt
    Replies:
    1
    Views:
    466
    Michael Dunn
    Nov 14, 2005
  2. Chris Dollin
    Replies:
    8
    Views:
    344
    Eric Sosman
    Feb 8, 2007
  3. Bruno Desthuilliers
    Replies:
    12
    Views:
    359
    Bruno Desthuilliers
    Oct 15, 2008
  4. itaj sherman
    Replies:
    1
    Views:
    296
    Daniel Pitts
    Feb 25, 2009
  5. James Allsopp
    Replies:
    11
    Views:
    458
    gwowen
    Apr 9, 2010
Loading...

Share This Page