473,226 Members | 1,664 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,226 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 2743
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: malcolm | last post by:
Example, suppose you have these 2 tables (NOTE: My example is totally different, but I'm simply trying to setup the a simpler version, so excuse the bad design; not the point here) CarsSold {...
4
by: bobsawyer | last post by:
I've been building a series of SELECT lists that are populated dynamically using HTTPRequest. Things are going pretty well, and I've got the whole thing working flawlessly in Mozilla/Firebird....
15
by: grunar | last post by:
After some thought on what I need in a Python ORM (multiple primary keys, complex joins, case statements etc.), and after having built these libraries for other un-named languages, I decided to...
10
by: Antanas | last post by:
The problem is that when AddID is used multiple times in the same select statement, it returns the same value in all places. How could I force function AddID to increment OBJECTID sequence? Here...
2
by: David Emme | last post by:
Access 97 I have a number of SELECT statements which contain references to user-defined VBA functions. These typically work as expected, but occasionally, on one user's machine or another,...
3
by: Marcia Hon | last post by:
Hi, I am trying to use the select() socket programming command to select between stdin and a connection. Currently, I have a listening stream and stdin that I insert into the fd_set. The problem...
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
2
by: Gary Dale | last post by:
I have a form with a pull-down list with six options, each of which has a value set. The value is the e-mail account name (without the domain) of a group while the displayed value is the full name...
5
by: ThatVBGuy | last post by:
Hello All, I have a multi-select list box, In this box is a list of names. I want to call a javascript onClick that will keep a running total of how many people were clicked. As soon as a user...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...

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.