Python Coolness

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:

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
 
Joined
Mar 22, 2007
Messages
1,463
Reaction score
67
Points
48
Location
UK
Your Mac's Specs
Lenovo Z560 Hackintosh -:- '06 iMac -:- iPod Touch 2ndGen
Now that is neat. PHP does that, or something similar, you can call variables as a function just by adding () after them - I did it by accident.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top