[Tutor] How to use function from specific module and then switch to other module

John Fouhy john at fouhy.net
Fri Nov 7 02:12:46 CET 2008


2008/11/7 Ertl, John C CIV 63134 <john.ertl at navy.mil>:
> The idea is as I step through a list I want to use a different function
> (same name but from a different module) for each element in the list.  How
> do I have a generic way to do this.
>
> for example for point 1 I want to use the rain function from Module A and
> then for point 2 I want to use the rain function from Module B.

Hi John,

You could use a dictionary to store the functions.  For example:

import module_a
import module_b

rain_functions = { 1:module_a.get_rain, 2:module_b.get_rain }

Then you could call the function as:

x = forecast()
points = x.getPoints()
for point in points:
    rain_functions[point]()

Hope this helps,

-- 
John.


More information about the Tutor mailing list