0

I want to create some kind of user interface and the user should be able to type the name of the function he wishes to run:

task = input('Programm to run: ')

Once task is defined I want to execute the desired task like:

task()

If for example the desired program is functonthe script shall execute function().

TheClockTwister
  • 819
  • 8
  • 21
  • 3
    If you're fine to stick to functions for now, your question was already answered on SO [Invoking top-level function by name in Python](http://stackoverflow.com/questions/12667537/invoking-op-level-function-by-name-in-python) – agg3l Oct 28 '16 at 22:20
  • 1
    The duplicate question is strictly for invoking top level functions which is not a great idea, and OP need not do that for their use case. The accepted answer allows 'calling' any variable in the top level scope which may be a bad idea. The other options have annoying boilerplate which mentions each function name thrice. – Alex Hall Oct 28 '16 at 22:27
  • 1
    @AlexHall - I don't think there's any problem with it. Personally, I would've used the popular [variable variables](http://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables-in-python) question, but the linked duplicate is a bit more specific. A dictionary is the popular way to do this. – TigerhawkT3 Oct 28 '16 at 22:29

3 Answers3

1

Here is another way for doing it globally:

def foo():
    print("foo")


def bar():
    print("bar")


method = input("What method: ")  # ask for method
globals().get(method, lambda: 0)()  # search for global method and execute it
mosaixz
  • 21
  • 2
1

Like said in the comments, you can create a dictionary and run the functions from it:

def func1(): print('I am f1')
def func2(): print('I am f2')
def func3(): print('I am f3')


functions = {'f1': func1, 'f2': func2, 'f3': func3}

x = input('which function:')
functions[x]()  # call the function
0

Here's one way:

class Tasks(object):
    def foo(self):
        print('hi')

    def bar(self):
        print('bye')


task = 'foo'

getattr(Tasks(), task)()
Alex Hall
  • 34,833
  • 5
  • 57
  • 89
  • A dictionary would probably be a more natural container for string-keyed functions than an instance in this situation. – Blckknght Oct 28 '16 at 22:27
  • Yes but that requires more boilerplate and name duplication. One might define a function and then forget to add it to the dictionary. – Alex Hall Oct 28 '16 at 22:28
  • Hmm, fair enough. Since your class is being used solely as a namespace for its methods and not to hold any data, perhaps you should make the methods static and look them up directly on the class, rather than on an instance? – Blckknght Oct 28 '16 at 22:29
  • I think it's a matter of opinion as to whether a string key for each function is more boilerplate than a class plus `getattr`. – TigerhawkT3 Oct 28 '16 at 22:34
  • @TigerhawkT3 it's constant boilerplate which you copy paste once. If there are 10 functions and you keep adding more the dictionary clearly loses. – Alex Hall Oct 28 '16 at 22:37