Select one of 2 functions with the same name ?

BJörn Lindqvist bjourne at gmail.com
Sun Jun 10 18:13:18 EDT 2007


On 6/10/07, Stef Mientki <S.Mientki-nospam at mailbox.kun.nl> wrote:
> I can realize it with a simple switch within each function,
> but that makes the code much less readable:
>
> def Some_Function():
>    if simulation_level == 1:
>      ... do things in a way
>    elif simulation_level == 2:
>      ... do things in another way
>    elif simulation_level == 3:
>      ... do things in yet another way

If you only have three levels, then that definitely is the best way to
solve the  problem. If you have more, and if they may change, then use
a dispatch-dict:

def simulator_1():
    print 'mooo'
...
simulators = {1 : simulartor_1, 2 : simulator_2, 3 : simulator_3}
def Some_Function():
    simulators[simulation_level]()


-- 
mvh Björn



More information about the Python-list mailing list