[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Question on importing and function defs

TC

3/2/2008 4:15:00 PM

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.
8 Answers

Nick Miller

3/2/2008 4:25:00 PM

0

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

Gary Herron

3/2/2008 4:37:00 PM

0

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

TC

3/2/2008 4:52:00 PM

0

On Mar 2, 11:37 am, Gary Herron <gher...@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()

Steve Holden

3/2/2008 5:44:00 PM

0

TC wrote:
> On Mar 2, 11:37 am, Gary Herron <gher...@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.hold...

Aaron Brady

3/2/2008 6:33:00 PM

0

On Mar 2, 11:44 am, Steve Holden <st...@holdenweb.com> wrote:
> TC wrote:
> > On Mar 2, 11:37 am, Gary Herron <gher...@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.holde... Hide quoted text -
>
> - Show quoted text -

You can use 'settrace' to intervene. You might be able to delete the
'a'.

John Manning

2/20/2014 2:14:00 PM

0

On 2/20/2014 5:53 AM, thomas p. wrote:
> "WangoTango" <Asgard24@mindspring.com> skrev i meddelelsen
> news:MPG.2d6f12564800bbb398be72@news.east.earthlink.net...
>> In article <EYGdnU6MLMLzUpnOnZ2dnUVZ_sCdnZ2d@giganews.com>,
>> jrobertm@terra.com.br says...
>>>
>>> Have a look: http://www.bartcop.com/gop-ha...
>> Both parties are entirely vested in keeping the system EXACTLY the way
>> it is. They have to keep the country as two warring factions lest some
>> other party get a foot in the door.
>>
>
> As long as you have the election system you have, two parties are what you
> will have. A third party will either push one of the others out or, the
> usual result, will disappear or be irrelevant. It is a very poor system,
> if one wants representative democracy.

Which has nothing to do with the topic.

Conservatives Stand For HATE

Even when it DIRECTLY INVOLVES their *OWN* FELLOW AMERICAN CITIZENS...

Conservatives hate American citizens who are unemployed, or poor, or
hungry, or weak or sick.

Conservatives hate the social safety nets that *WE ALL* contribute to as
American citizens.

Conservatives hate equitably funded K-12 public education for ALL
Americans whether rich or poor.

Conservatives hate American citizens who are highly educated, calling
them 'elitists'.

Conservatives hate American citizens who are gay.

Conservatives hate American citizens who are immigrants.

Conservatives hate American citizens who are Muslims.

Conservatives hate American citizens who are African Americans.

Conservatives hate American citizens who are atheists.

Conservatives hate American citizens who are liberals.

Conservatives hate American citizens who Democrats.

Conservatives hate ANY "others" among American citizens who are not like
them.

Conservatives hate science.

Conservatives even hate their own government of the United States of
America.

*THEN* they call themselves 'Patriots'.

*Most of them even shamelessly call themselves Christians. *


Mitchell Holman

2/20/2014 2:25:00 PM

0

John Manning <jrobertm@terra.com.br> wrote in
news:scmdnSS7_5MDkJvOnZ2dnUVZ_u-dnZ2d@giganews.com:

> Conservatives hate American citizens who are highly educated,
> calling them 'elitists'.
> <br>
> <br>
> Conservatives hate American citizens who are gay.
> <br>
> <br>
> Conservatives hate American citizens who are immigrants.
> <br>
> <br>
> Conservatives hate American citizens who are Muslims.
> <br>
> <br>
> Conservatives hate American citizens who are African Americans.
> <br>
> <br>
> Conservatives hate American citizens who are atheists.
> <br>
> <br>
> Conservatives hate American citizens who are liberals.
> <br>
> <br>
> Conservatives hate American citizens who Democrats.
> <br>
> <br>
> Conservatives hate ANY "others" among American citizens who are
> not like them.
> <br>
> <br>
> Conservatives hate science.
> <br>
> <br>
> Conservatives even hate their own government of the United States
> of America.
> <br>



Modern Conservative: Someone who can take time
out from bragging about how much he loves America
to circulate secession petitions that would destroy
America.





logan.sacket

2/20/2014 2:28:00 PM

0

On Thu, 20 Feb 2014 09:53:04 +0100, "thomas p." <gudloos@yahoo.com>
wrote:

>"WangoTango" <Asgard24@mindspring.com> skrev i meddelelsen
>news:MPG.2d6f12564800bbb398be72@news.east.earthlink.net...
>> In article <EYGdnU6MLMLzUpnOnZ2dnUVZ_sCdnZ2d@giganews.com>,
>> jrobertm@terra.com.br says...
>>>
>>>
>>> Have a look: http://www.bartcop.com/gop-ha...
>>
>> Both parties are entirely vested in keeping the system EXACTLY the way
>> it is. They have to keep the country as two warring factions lest some
>> other party get a foot in the door.
>>
>
>
>As long as you have the election system you have, two parties are what you
>will have. A third party will either push one of the others out or, the
>usual result, will disappear or be irrelevant. It is a very poor system,
>if one wants representative democracy.

And as long as the Republicans act like a me-to party, nothing will
really change anyway. We are toast.