Question on importing and function defs

Steve Holden steve at holdenweb.com
Sun Mar 2 12:44:13 EST 2008


TC wrote:
> On Mar 2, 11:37 am, Gary Herron <gher... at islandtraining.com> 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/




More information about the Python-list mailing list