Select one of 2 functions with the same name ?

S

Stef Mientki

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
 
7

7stud

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

Try something like this:

class Simulation1(object):
def greet(self):
print "hello"

class Simulation2(object):
def greet(self):
print "hello"

class Simulation3(object):
def greet(self):
print "hello"


def someFunc(simObj):
simObj.greet()


s1 = Simulation1()
s2 = Simulation2()
s3 = Simulation3()

someFunc(s1)
someFunc(s2)
someFunc(s3)
 
F

Francesco Guerrieri

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 :)



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


--
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as
both victim and villain by the vicissitudes of fate. This visage, no
mere veneer of vanity, is a vestige of the vox populi, now vacant,
vanished. However, this valorous visitation of a bygone vexation
stands vivified, and has vowed to vanquish these venal and virulent
vermin vanguarding vice and vouchsafing the violently vicious and
voracious violation of volition. The only verdict is vengeance; a
vendetta held as a votive, not in vain, for the value and veracity of
such shall one day vindicate the vigilant and the virtuous. Verily,
this vichyssoise of verbiage veers most verbose vis-à-vis an
introduction, so let me simply add that it's my very good honor to
meet you and you may call me V." -- V's introduction to Evey
 
7

7stud

Try something like this:

class Simulation1(object):
def greet(self):
print "hello"

class Simulation2(object):
def greet(self):
print "hello"

class Simulation3(object):
def greet(self):
print "hello"

def someFunc(simObj):
simObj.greet()

s1 = Simulation1()
s2 = Simulation2()
s3 = Simulation3()

someFunc(s1)
someFunc(s2)
someFunc(s3)

Horrible example. Look at this instead:

class Simulation1(object):
def greet(self):
print "hello from sim1"

class Simulation2(object):
def greet(self):
print "Hi. I'm sim2"

class Simulation3(object):
def greet(self):
print "sim3 is #1! Hello there."


def someFunc(simObj):
simObj.greet()


s1 = Simulation1()
s2 = Simulation2()
s3 = Simulation3()

someFunc(s1)
someFunc(s2)
someFunc(s3)


---output:---
hello from sim1
Hi. I'm sim2
sim3 is #1! Hello there.

As the output shows, you can call the same function, but the function
can do different things.
 
S

Stef Mientki

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 said:
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 :)
 
7

7stud

thanks Francesco and "7stud",

The solution with objects is too difficult,
because I want to stay very close to the orginal language,

Why would you want to duplicate poorly written code?
 
?

=?ISO-8859-1?Q?BJ=F6rn_Lindqvist?=

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]()
 
S

Stef Mientki

7stud said:
Why would you want to duplicate poorly written code?
I didn't know that a program written without OOP's is "poorly written" ;-)

The orginal language is thé best language available for that particular micro.

cheers,
Stef
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,690
Members
48,796
Latest member
Greg L.

Latest Threads

Top