Select one of 2 functions with the same name ?
7stud
bbxx789_05ss at yahoo.com
Sun Jun 10 13:44:35 EDT 2007
On Jun 10, 11:11 am, 7stud <bbxx789_0... at yahoo.com> wrote:
> On Jun 10, 10:37 am, Stef Mientki <S.Mientki-nos... 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
>
> 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.
More information about the Python-list
mailing list