Question on importing and function defs
Gary Herron
gherron at islandtraining.com
Sun Mar 2 11:37:11 EST 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
More information about the Python-list
mailing list