471,348 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,348 software developers and data experts.

Select one of 2 functions with the same name ?

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
Jun 10 '07 #1
7 2665
On Jun 10, 10:37 am, Stef Mientki <S.Mientki-nos...@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
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)

Jun 10 '07 #2
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.**************@mailbox.kun.nlwrote:
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

--
"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
Jun 10 '07 #3
On Jun 10, 11:11 am, 7stud <bbxx789_0...@yahoo.comwrote:
On Jun 10, 10:37 am, Stef Mientki <S.Mientki-nos...@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

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.

Jun 10 '07 #4
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.**************@mailbox.kun.nlwrote:
>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

Jun 10 '07 #5
On Jun 10, 2:03 pm, Stef Mientki <S.Mientki-nos...@mailbox.kun.nl>
wrote:
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?

Jun 10 '07 #6
On 6/10/07, Stef Mientki <S.**************@mailbox.kun.nlwrote:
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
Jun 10 '07 #7
7stud wrote:
On Jun 10, 2:03 pm, Stef Mientki <S.Mientki-nos...@mailbox.kun.nl>
wrote:
>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?
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
Jun 10 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by bobsawyer | last post: by
3 posts views Thread by Marcia Hon | last post: by
22 posts views Thread by MP | last post: by
2 posts views Thread by Gary Dale | last post: by
1 post views Thread by Ronak mishra | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.