473,396 Members | 1,864 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,396 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 2746
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.