python function that returns a function from list of functions

Posted by thkang on Stack Overflow See other posts from Stack Overflow or by thkang
Published on 2012-12-02T03:33:28Z Indexed on 2012/12/02 5:04 UTC
Read the original article Hit count: 215

Filed under:
|

I want to make following function:

 1)input is a number. 
 2)functions are indexed, return a function whose index matches given number

here's what I came up with:

def foo_selector(whatfoo):
    def foo1():
        return
    def foo2():
        return
    def foo3():
        return
    ...

    def foo999():
        return

    #something like return foo[whatfoo]

the problem is, how can I index the functions (foo#)? I can see functions foo1 to foo999 by dir(). however, dir() returns name of such functions, not the functions themselves. In the example, those foo-functions aren't doing anything. However in my program they perform different tasks and I can't automatically generate them. I write them myself, and have to return them by their name.

© Stack Overflow or respective owner

Related posts about python

Related posts about function