Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Question on importing and function defs

Reply
Thread Tools

Question on importing and function defs

 
 
TC
Guest
Posts: n/a
 
      03-02-2008
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.
 
Reply With Quote
 
 
 
 
Nick Miller
Guest
Posts: n/a
 
      03-02-2008
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

 
Reply With Quote
 
 
 
 
Gary Herron
Guest
Posts: n/a
 
      03-02-2008
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

 
Reply With Quote
 
TC
Guest
Posts: n/a
 
      03-02-2008
On Mar 2, 11:37 am, Gary Herron <(E-Mail Removed)> 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()
 
Reply With Quote
 
Steve Holden
Guest
Posts: n/a
 
      03-02-2008
TC wrote:
> On Mar 2, 11:37 am, Gary Herron <(E-Mail Removed)> 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/

 
Reply With Quote
 
castironpi@gmail.com
Guest
Posts: n/a
 
      03-02-2008
On Mar 2, 11:44*am, Steve Holden <(E-Mail Removed)> wrote:
> TC wrote:
> > On Mar 2, 11:37 am, Gary Herron <(E-Mail Removed)> 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'.
 
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
Arrays and defs Jens Finnäs Ruby 5 02-04-2011 08:54 PM
python function defs/declarations bruce Python 3 07-05-2006 02:00 PM
accesing a constants file from tiles-defs.xml nooobody Java 0 11-30-2004 05:47 AM
Must function defs appear earlier than calls? SeeBelow@SeeBelow.Nut Python 8 05-05-2004 01:55 AM
SpywareBlaster New Defs Today Buffalo Computer Support 2 10-01-2003 12:20 AM



Advertisments