- Joined
- Jan 20, 2007
- Messages
- 3,269
- Reaction score
- 270
- Points
- 83
- Location
- Oakton, VA USA
- Your Mac's Specs
- White MacBook Intel C2D 2.2GHz, 2G, 250G, SD, Leopard.
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:
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