Thread: Python Coolness
View Single Post
mathogre

 
mathogre's Avatar
 
Member Since: Jan 20, 2007
Location: Oakton, VA USA
Posts: 3,269
mathogre has a brilliant futuremathogre has a brilliant futuremathogre has a brilliant futuremathogre has a brilliant futuremathogre has a brilliant futuremathogre has a brilliant futuremathogre has a brilliant futuremathogre has a brilliant futuremathogre has a brilliant futuremathogre has a brilliant futuremathogre has a brilliant future
Mac Specs: White MacBook Intel C2D 2.2GHz, 2G, 250G, SD, Leopard.

mathogre is offline
Python is one of the coolest languages.

I was just playing around with it today, beginning a little program I'm writing for fun. Problem was that I'd like to be able to easily call functions based on some condition. "Easily" is the operative term.

And it *is* easy. Define the functions, place their names in a named list, and then call the list item as a function of condition. Here's a simple example:

Code:
>>> def ab() :
...    return 0
... 
>>> def cd() :
...    return 1
... 
>>> def ef(q, r) :
...    return q+r
... 
>>> gh = [ab, cd, ef]
>>> 
>>> gh
[<function ab at 0x61270>, <function cd at 0x612b0>, <function ef at 0x612f0>]
>>> 
>>> gh[0]()
0
>>> 
>>> gh[1]()
1
>>> 
>>> gh[2](4, 9)
13

Evil Math Ogre Kgh-Ra
Integrate me. Differentiate me.
You can't hurt me. I'm e^x. Ha ha ha!

Homepage | Gallery
I was on the M-F honor roll for July 2007.
QUOTE Thanks