Question on importing and function defs

T

TC

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.
 
N

Nick Miller

TC said:
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
 
G

Gary Herron

TC said:
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
 
T

TC

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()
 
S

Steve Holden

TC said:
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
 
C

castironpi

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'.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top