Mac Forums

Mac Forums (http://www.mac-forums.com/forums/)
-   OS X - Development and Darwin (http://www.mac-forums.com/forums/os-x-development-darwin/)
-   -   Python Coolness (http://www.mac-forums.com/forums/os-x-development-darwin/61831-python-coolness.html)

mathogre 05-07-2007 10:56 AM

Python Coolness
 
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


knightlie 05-07-2007 05:19 PM

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.


All times are GMT -4. The time now is 04:58 PM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.