Select one of 2 functions with the same name ?

Stef Mientki S.Mientki-nospam at mailbox.kun.nl
Sun Jun 10 22:03:58 CEST 2007


thanks Francesco and "7stud",

The solution with objects is too difficult,
because I want to stay very close to the orginal language,
( so the users can understand the Python code,
   and the automatic translation becomes as simple as possible).

But after some tests, it seems to be quit simple:

<Python>
   simulation_level = 0

   def f1():
     print 'f1'

   if simulation_level == 2:
     def f1():
       print 'f2'

   f1()
</Python>


cheers,
Stef Mientki

Francesco Guerrieri wrote:
> If the functions are
> f1, f2, f3  you could go this way:
> 
> def SimulationRun():
>    if simulation_level = 1: SimulationFunction = f1
>    else if simulation_level = 2: SimulationFunction = f2
>    else ....
> 
> 
> and in the rest of the code you can refer to SimulationFunction
> instead of explicitly calling f1, f2 or f3.
> 
> I'm sure that there are many smarter methods :-)
> 
> 
> 
> On 6/10/07, Stef Mientki <S.Mientki-nospam at mailbox.kun.nl> wrote:
>> hello,
>>
>> For a simulation at different levels,
>> I need different functions with the same name.
>> Is that possible ?
>>
>> 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
>>
>>
>> thanks,
>> Stef Mientki
>> -- 
>> http://mail.python.org/mailman/listinfo/python-list
>>
> 
> 



More information about the Python-list mailing list