Question on importing and function defs

Discussion in 'Python' started by TC, Mar 2, 2008.

  1. TC

    TC Guest

    I have a problem. Here's a simplified version of what I'm doing:

    I have functions a() and b() in a module called 'mod'. b() calls a().

    So now, I have this program:

    from mod import *

    def a():
    blahblah

    b()


    The problem being, b() is calling the a() that's in mod, not the new
    a() that I want to replace it. (Both a()'s have identical function
    headers, in case that matters.) How can I fix this?

    Thanks for any help.
    TC, Mar 2, 2008
    #1
    1. Advertising

  2. TC

    Nick Miller Guest

    TC wrote:
    > I have a problem. Here's a simplified version of what I'm doing:
    >
    > I have functions a() and b() in a module called 'mod'. b() calls a().
    >
    > So now, I have this program:
    >
    > from mod import *
    >
    > def a():
    > blahblah
    >
    > b()
    >
    >
    > The problem being, b() is calling the a() that's in mod, not the new
    > a() that I want to replace it. (Both a()'s have identical function
    > headers, in case that matters.) How can I fix this?
    >
    > Thanks for any help.
    >


    The only problem I could see with this is a local function issue.
    Meaning that even when you write the new a() function in the main source
    file, b() doesn't know it exists because it's relying on it's own
    "local" a() in the module. I'm also new to using Python so I that's all
    I can think would be the problem.

    Nick
    Nick Miller, Mar 2, 2008
    #2
    1. Advertising

  3. TC

    Gary Herron Guest

    TC wrote:
    > I have a problem. Here's a simplified version of what I'm doing:
    >
    > I have functions a() and b() in a module called 'mod'. b() calls a().
    >
    > So now, I have this program:
    >
    > from mod import *
    >
    > def a():
    > blahblah
    >
    > b()
    >
    >
    > The problem being, b() is calling the a() that's in mod, not the new
    > a() that I want to replace it. (Both a()'s have identical function
    > headers, in case that matters.) How can I fix this?
    >
    > Thanks for any help.
    >


    Since b calls mod.a, you could replace mod.a with your new a. Like
    this: (Warning, this could be considered bad style because it will
    confuse anyone who examines the mod module in an attempt to understand
    you code.)


    import mod

    def replacement_a():
    ...

    mod.a = replacement_a

    ...


    Or another option. Define b to take, as a parameter, the "a" function
    to call.

    In mod:

    def a():
    ...

    def b(fn=a): # to set the default a to call
    ...

    And you main program:

    from mod import *

    def my_a():
    ...

    b(my_a)


    Hope that helps

    Gary Herron
    Gary Herron, Mar 2, 2008
    #3
  4. TC

    TC Guest

    On Mar 2, 11:37 am, Gary Herron <> wrote:
    > TC wrote:
    > > I have a problem. Here's a simplified version of what I'm doing:

    >
    > > I have functions a() and b() in a module called 'mod'. b() calls a().

    >
    > > So now, I have this program:

    >
    > > from mod import *

    >
    > > def a():
    > > blahblah

    >
    > > b()

    >
    > > The problem being, b() is calling the a() that's in mod, not the new
    > > a() that I want to replace it. (Both a()'s have identical function
    > > headers, in case that matters.) How can I fix this?

    >
    > > Thanks for any help.

    >
    > Since b calls mod.a, you could replace mod.a with your new a. Like
    > this: (Warning, this could be considered bad style because it will
    > confuse anyone who examines the mod module in an attempt to understand
    > you code.)
    >
    > import mod
    >
    > def replacement_a():
    > ...
    >
    > mod.a = replacement_a
    >
    > ...
    >
    > Or another option. Define b to take, as a parameter, the "a" function
    > to call.
    >
    > In mod:
    >
    > def a():
    > ...
    >
    > def b(fn=a): # to set the default a to call
    > ...
    >
    > And you main program:
    >
    > from mod import *
    >
    > def my_a():
    > ...
    >
    > b(my_a)
    >
    > Hope that helps
    >
    > Gary Herron


    Thanks for the tips, but no luck. This is for a homework assignment,
    so there are a couple of requirements, namely that I can't touch
    'mod', and I have to do 'from mod import *' as opposed to 'import
    mod'.

    So the first method you suggested won't work as written, since the mod
    namespace doesn't exist. I tried a = replacement_a, but b() is still
    calling mod's version of a() for some reason. And because I can't
    touch mod, I can't use your second suggestion.

    In case I somehow oversimplified, here's the actual relevant code, in
    'mod' (actually called 'search'). The first fn is what I've been
    calling a(), the second is b().

    (lots of stuff...)

    def compare_searchers(problems, header,
    searchers=[breadth_first_tree_search,
    breadth_first_graph_search,
    depth_first_graph_search,
    iterative_deepening_search,
    depth_limited_search,
    astar_search]):
    def do(searcher, problem):
    p = InstrumentedProblem(problem)
    searcher(p)
    return p
    table = [[name(s)] + [do(s, p) for p in problems] for s in
    searchers]
    print_table(table, header)

    def compare_graph_searchers():
    compare_searchers(problems=[GraphProblem('A', 'B', romania),
    GraphProblem('O', 'N', romania),
    GraphProblem('Q', 'WA', australia)],
    header=['Searcher', 'Romania(A,B)', 'Romania(O, N)',
    'Australia'])


    That's the end of the 'search' file. And here's my program, which
    defines an identical compare_searchers() with an added print
    statement. That statement isn't showing up.

    from search import *

    def compare_searchers(problems, header,
    searchers=[breadth_first_tree_search,
    breadth_first_graph_search,
    depth_first_graph_search,
    iterative_deepening_search,
    depth_limited_search,
    astar_search, best_first_graph_search]):
    def do(searcher, problem):
    p = InstrumentedProblem(problem)
    searcher(p)
    return p
    table = [[name(s)] + [do(s, p) for p in problems] for s in
    searchers]
    print 'test'
    print_table(table, header)

    compare_graph_searchers()
    TC, Mar 2, 2008
    #4
  5. TC

    Steve Holden Guest

    TC wrote:
    > On Mar 2, 11:37 am, Gary Herron <> wrote:
    >> TC wrote:
    >>> I have a problem. Here's a simplified version of what I'm doing:
    >>> I have functions a() and b() in a module called 'mod'. b() calls a().
    >>> So now, I have this program:
    >>> from mod import *
    >>> def a():
    >>> blahblah
    >>> b()
    >>> The problem being, b() is calling the a() that's in mod, not the new
    >>> a() that I want to replace it. (Both a()'s have identical function
    >>> headers, in case that matters.) How can I fix this?
    >>> Thanks for any help.

    >> Since b calls mod.a, you could replace mod.a with your new a. Like
    >> this: (Warning, this could be considered bad style because it will
    >> confuse anyone who examines the mod module in an attempt to understand
    >> you code.)
    >>
    >> import mod
    >>
    >> def replacement_a():
    >> ...
    >>
    >> mod.a = replacement_a
    >>
    >> ...
    >>
    >> Or another option. Define b to take, as a parameter, the "a" function
    >> to call.
    >>
    >> In mod:
    >>
    >> def a():
    >> ...
    >>
    >> def b(fn=a): # to set the default a to call
    >> ...
    >>
    >> And you main program:
    >>
    >> from mod import *
    >>
    >> def my_a():
    >> ...
    >>
    >> b(my_a)
    >>
    >> Hope that helps
    >>
    >> Gary Herron

    >
    > Thanks for the tips, but no luck. This is for a homework assignment,
    > so there are a couple of requirements, namely that I can't touch
    > 'mod', and I have to do 'from mod import *' as opposed to 'import
    > mod'.
    >
    > So the first method you suggested won't work as written, since the mod
    > namespace doesn't exist. I tried a = replacement_a, but b() is still
    > calling mod's version of a() for some reason. And because I can't
    > touch mod, I can't use your second suggestion.
    >
    > In case I somehow oversimplified, here's the actual relevant code, in
    > 'mod' (actually called 'search'). The first fn is what I've been
    > calling a(), the second is b().
    >
    > (lots of stuff...)
    >
    > def compare_searchers(problems, header,
    > searchers=[breadth_first_tree_search,
    > breadth_first_graph_search,
    > depth_first_graph_search,
    > iterative_deepening_search,
    > depth_limited_search,
    > astar_search]):
    > def do(searcher, problem):
    > p = InstrumentedProblem(problem)
    > searcher(p)
    > return p
    > table = [[name(s)] + [do(s, p) for p in problems] for s in
    > searchers]
    > print_table(table, header)
    >
    > def compare_graph_searchers():
    > compare_searchers(problems=[GraphProblem('A', 'B', romania),
    > GraphProblem('O', 'N', romania),
    > GraphProblem('Q', 'WA', australia)],
    > header=['Searcher', 'Romania(A,B)', 'Romania(O, N)',
    > 'Australia'])
    >
    >
    > That's the end of the 'search' file. And here's my program, which
    > defines an identical compare_searchers() with an added print
    > statement. That statement isn't showing up.
    >
    > from search import *
    >
    > def compare_searchers(problems, header,
    > searchers=[breadth_first_tree_search,
    > breadth_first_graph_search,
    > depth_first_graph_search,
    > iterative_deepening_search,
    > depth_limited_search,
    > astar_search, best_first_graph_search]):
    > def do(searcher, problem):
    > p = InstrumentedProblem(problem)
    > searcher(p)
    > return p
    > table = [[name(s)] + [do(s, p) for p in problems] for s in
    > searchers]
    > print 'test'
    > print_table(table, header)
    >
    > compare_graph_searchers()


    Since you've admitted it's for homework, here are a couple of hints.

    1. The b() function is *always* going to try and resolve its references
    in the namespace it was defined in;

    2. The technique you need is most likely known as "monkey patching".
    When you say "I can't touch mod", that may mean "the source of mod must
    remain unchanged", which is subtly different. Google is your friend ...

    Good luck with your assignment.

    regards
    Steve
    --
    Steve Holden +1 571 484 6266 +1 800 494 3119
    Holden Web LLC http://www.holdenweb.com/
    Steve Holden, Mar 2, 2008
    #5
  6. TC

    Guest

    On Mar 2, 11:44 am, Steve Holden <> wrote:
    > TC wrote:
    > > On Mar 2, 11:37 am, Gary Herron <> wrote:
    > >> TC wrote:
    > >>> I have a problem.  Here's a simplified version of what I'm doing:
    > >>> I have functions a() and b() in a module called 'mod'.  b() calls a().
    > >>> So now, I have this program:
    > >>> from mod import *
    > >>> def a():
    > >>>     blahblah
    > >>> b()
    > >>> The problem being, b() is calling the a() that's in mod, not the new
    > >>> a() that I want to replace it.  (Both a()'s have identical function
    > >>> headers, in case that matters.)  How can I fix this?
    > >>> Thanks for any help.
    > >> Since b calls mod.a, you could replace mod.a with your new a.  Like
    > >> this:  (Warning, this could be considered bad style because it will
    > >> confuse anyone who examines the mod module in an attempt to understand
    > >> you code.)

    >
    > >>   import mod

    >
    > >>   def replacement_a():
    > >>     ...

    >
    > >>   mod.a = replacement_a

    >
    > >>   ...

    >
    > >> Or another option.  Define b to take, as a parameter, the "a" function
    > >> to call.

    >
    > >> In mod:

    >
    > >>   def a():
    > >>    ...

    >
    > >>   def b(fn=a):  # to set the default a to call
    > >>     ...

    >
    > >> And you main program:

    >
    > >>   from mod import *

    >
    > >>   def my_a():
    > >>     ...

    >
    > >>   b(my_a)

    >
    > >> Hope that helps

    >
    > >> Gary Herron

    >
    > > Thanks for the tips, but no luck.  This is for a homework assignment,
    > > so there are a couple of requirements, namely that I can't touch
    > > 'mod', and I have to do 'from mod import *' as opposed to 'import
    > > mod'.

    >
    > > So the first method you suggested won't work as written, since the mod
    > > namespace doesn't exist.  I tried a = replacement_a, but b() is still
    > > calling mod's version of a() for some reason.  And because I can't
    > > touch mod, I can't use your second suggestion.

    >
    > > In case I somehow oversimplified, here's the actual relevant code, in
    > > 'mod' (actually called 'search').  The first fn is what I've been
    > > calling a(), the second is b().

    >
    > > (lots of stuff...)

    >
    > > def compare_searchers(problems, header,
    > > searchers=[breadth_first_tree_search,
    > >                       breadth_first_graph_search,
    > > depth_first_graph_search,
    > >                       iterative_deepening_search,
    > > depth_limited_search,
    > >                       astar_search]):
    > >     def do(searcher, problem):
    > >         p = InstrumentedProblem(problem)
    > >         searcher(p)
    > >         return p
    > >     table = [[name(s)] + [do(s, p) for p in problems] for s in
    > > searchers]
    > >     print_table(table, header)

    >
    > > def compare_graph_searchers():
    > >     compare_searchers(problems=[GraphProblem('A', 'B', romania),
    > >                                 GraphProblem('O', 'N', romania),
    > >                                 GraphProblem('Q', 'WA', australia)],
    > >             header=['Searcher', 'Romania(A,B)', 'Romania(O, N)',
    > > 'Australia'])

    >
    > > That's the end of the 'search' file.  And here's my program, which
    > > defines an identical compare_searchers() with an added print
    > > statement.  That statement isn't showing up.

    >
    > > from search import *

    >
    > > def compare_searchers(problems, header,
    > > searchers=[breadth_first_tree_search,
    > >                       breadth_first_graph_search,
    > > depth_first_graph_search,
    > >                       iterative_deepening_search,
    > > depth_limited_search,
    > >                       astar_search, best_first_graph_search]):
    > >     def do(searcher, problem):
    > >         p = InstrumentedProblem(problem)
    > >         searcher(p)
    > >         return p
    > >     table = [[name(s)] + [do(s, p) for p in problems] for s in
    > > searchers]
    > >     print 'test'
    > >     print_table(table, header)

    >
    > > compare_graph_searchers()

    >
    > Since you've admitted it's for homework, here are a couple of hints.
    >
    > 1. The b() function is *always* going to try and resolve its references
    > in the namespace it was defined in;
    >
    > 2. The technique you need is most likely known as "monkey patching".
    > When you say "I can't touch mod", that may mean "the source of mod must
    > remain unchanged", which is subtly different. Google is your friend ...
    >
    > Good luck with your assignment.
    >
    > regards
    >   Steve
    > --
    > Steve Holden        +1 571 484 6266   +1 800 494 3119
    > Holden Web LLC              http://www.holdenweb.com/- Hide quoted text -
    >
    > - Show quoted text -


    You can use 'settrace' to intervene. You might be able to delete the
    'a'.
    , Mar 2, 2008
    #6
    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. nooobody
    Replies:
    0
    Views:
    503
    nooobody
    Nov 30, 2004
  2. Replies:
    8
    Views:
    286
  3. bruce
    Replies:
    3
    Views:
    347
  4. Stephen Mayes

    Standard return defs?

    Stephen Mayes, Aug 11, 2004, in forum: C Programming
    Replies:
    4
    Views:
    314
    Joona I Palaste
    Aug 12, 2004
  5. Jens Finnäs

    Arrays and defs

    Jens Finnäs, Feb 4, 2011, in forum: Ruby
    Replies:
    5
    Views:
    100
    Brian Candler
    Feb 4, 2011
Loading...

Share This Page