#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    40
    Rep Power
    11

    call a function using user input


    Hi,
    is it possible to call a function using an user input.
    eg:

    #def greet():
    # print 'Hello'
    #def today():
    # print 'today'

    x = raw_input("call func ")
    print x

    i.e if user inputs greet can it call func greet().
    pl. help
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    Hi!

    Code:
    x = input("call func ")
    x()
    Regards,
    mawe
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    40
    Rep Power
    11
    hi,
    sorry mawe but it doesnt work

    Code:
    def greet():
    print 'Hello'
    def bye():
    print 'bye'

    x = input("call func ")
    x()

    -any other suggestions.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    35
    Rep Power
    10
    if x == "greet":
    greet()
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    40
    Rep Power
    11
    hi,
    is it possible to call a function from a list of functions using user input as string value without assigning value to that func. i require it because the program which i am doing right now requires about 100 functions. i do not wish to go on assigning values to all those functions. the program is done in tkinter.therefore i wish to call function when i select it from listbox.

    --- any suggestions.
    Thank you
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    Hi!

    Originally Posted by phneoix
    sorry mawe but it doesnt work
    Do you get any error messages? I'm just asking because it works for me.

    Regards,
    mawe
  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    You could of course use exec/eval/input but all of these tend to come with a nice big security risk. So I've gone with using a 'functions' dict for this example since giving users access to all the functions defined by your program would be a bad idea .

    Code:
    >>> def foo():
    ...     print 'foo'
    ... 
    >>> def bar():
    ...     print 'bar'
    ...           
    >>> functions = {'foo': foo, 'bar': bar}
    >>> functions
    {'foo': <function foo at 0x61430>, 'bar': <function bar at 0x61a30>}
    >>> functions['foo'].__call__()
    foo
    >>> functions['foo']()
    foo
    >>> def funInput(prompt = ''):
    ...     function = raw_input(prompt)
    ...     if function in functions: return functions[function]
    ... 
    >>> x = funInput('call func: ')
    call func: bar
    >>> x()
    bar
    >>>
    In any case, thats all you need really need to do . And as long as you don't add any dangerous functions to functions you should be fine security wise.

    Enjoy,

    Mark.
    programming language development: www.netytan.com Ė Hula

  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    40
    Rep Power
    11
    hi,
    thanks netyan. the solution which u have given using dictionary method is good. but i would like to know, whether it is possible to call a function directly without assigning it in dictionary by just typing the name of function.

    for mawe - the error displayed is
    NameError: name 'greet' is not defined.
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    51
    Hi!

    This drives me crazy!
    Code:
    def greet():
    	print "hello"
    
    def bye():
    	print "bye"
    
    x = input("call func ")
    x()
    As I said, it works here. Try copy/paste it, perhaps you have a typo

    Regards,
    mawe
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2004
    Posts
    40
    Rep Power
    11
    hello mawe,
    found the problem source. i was using activestate pythonwin IDE.
    i wrote the program in default python IDLE & saved it. it runs flawlessly while the same program gives me an error while running in activestate. Think i have to start using IDLE again.though it seems a ridiculous thing to happen.

    thanks again mawe.
  20. #11
  21. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Here's a slightly extended version of the funInput() function that takes the dictionary an extra argument. This isn't as safe as before but it's still safer than using the input() function directly .

    Code:
    >>> def funInput(prompt = '', members = globals()):
    ...     member = raw_input(prompt)
    ...     if member in members and callable(members[member]):
    ...         return members[member]
    ... 
    >>> z = callInput('call func: ')
    call func: noFunction
    >>> print z
    None
    >>> z()
    
    Traceback (most recent call last):
      File "<pyshell#30>", line 1, in -toplevel-
        z()
    TypeError: 'NoneType' object is not callable
    >>>
    All this is really doing is prompting the user for a name and returning it from the members dictionary; in this case the globals(). If the name isnít found or isnít callable None is returned. Anyway I'd probably stick with the example form before unless you know that there's no chance of tampering.

    Enjoy,

    Mark.
    programming language development: www.netytan.com Ė Hula


IMN logo majestic logo threadwatch logo seochat tools logo