Store functions in list and call them later

call function from list python
functions inside a list python
store functions in dictionary
python list functions of object
storing functions in arrays python
python add function to list
python function as variable
how to call a function using object in python

I want to store functions in a list and then later in a program call those functions from that list.

This works for me, however, I don't know if it is correct :

#example functions, my functions would actually get user input etc.
def func1():
    return 2
def func2():
    return 3

options = [ func1, func2 ]

#call functions


Is this the proper way to store functions in a list and call it ? Or is there a different, better way to do this?

Yes, you can do it. If you want to call all functions in the list with a "one liner" you can do the following:

results = [f() for f in options]

Functions as Objects in Python - Python Pandemonium, Functions can be stored as elements of a list or any other data structure in This can be done by defining a __call__ function inside the object. To call foo, you can just do dispatcher['foo']() EDIT: If you want to run multiple functions stored in a list, you can possibly do something like this. dispatcher = {'foobar': [foo, bar], 'bazcat': [baz, cat]} def fire_all(func_list): for f in func_list: f() fire_all(dispatcher['foobar'])

Yes, this is correct, though if your functions are really one-liner in nature, I would suggest you also have a look at lambdas, so that your entire code can be reduced to:

options = [lambda: 3, lambda: 2]

As already pointed out in comments, you will Of course need to remember the order of the functions in the list.

Why store a function inside a python dictionary?, Then the following expressions are equivalent. You can call the function directly, or by referencing the dict element whose value is the function. >>> find_city  How to store the result of an executed function and re-use later? This would store the last 32 calls to get_pep and when it is back them up with references or

I like to do things like

class Commander:
    def CommandERROR(*args,**kwargs):
       print "Invalid command"
    def CommandFunc1(*args,**kwargs):
       print "Do Command 1"
    def CommandFunc2(*args,**kwargs):
       print "Do Command 2"
    def CallCommand(command,*args,**kwargs):

cmd = Commander()

as pointed out in the comments you can also put this in its own file to create the namespace and ommit the class ... although if you need to affect state then maybe a class is a better option ... you could also use a simple dict

Passing a function as an argument to another function in Python , Can a list be passed as an argument to a function? In the body of the function, tz and sz are just like any other variable. When the call is made to drawRectangle, the values in variables tx and sz are fetched first, then the call happens. So as we enter the top of function drawRectangle, its variable t is assigned the tess object, and w and h in that function are both given the value 50.

Not sure how others do it, but I do it the same way. It's perfect for my basic codes, hence I believe they can be used elsewhere too.

Python Passing a List as an Argument, You can even call a function object stored in the list without assigning it to a variable first. You can do the lookup and then immediately call the resulting  Python list is a sequence of values, it can be any type, strings, numbers, floats, mixed content, or whatever. In this post, we will talk about Python list functions and how to create, add elements, append, reverse, and many other Python list functions.

Python's Functions Are First-Class –, Code Reuse: Functions and Modules Reusing code is key to building a optionally empty list of arguments (enclosed in parentheses), a colon, and then one or more Create a folder called mymodules , which we'll use to store your modules. Once you have created a PowerShell profile, place the function in the profile and save and close. Now every time you open your PowerShell console the function will be available. 3) Directly In A Script. If you wish to use the function in a script, place the function in the script above the sections where you need to use it.

4. Code Reuse: Functions and Modules, In the following example, makeNoise does not list any parameter names, A return keyword without an expression after it will cause the function to return undefined . The place where the computer stores this context is the call stack. The technique is when you have a python dictionary and a function that you intend to use on it. You insert an extra element into the dict, whose value is the name of the function. When you're ready to call the function you issue the call indirectly by referring to the dict element, not the function by name.

Functions :: Eloquent JavaScript, You can store multiple function handles in an array, and save and load them, If the function does not require any inputs, then you can call the function with and a comma-separated list of input arguments to the anonymous function, arglist . I'm first call to user defined function! Again second call to the same function Pass by reference vs value. All parameters (arguments) in the Python language are passed by reference. It means if you change what a parameter refers to within a function, the change also reflects back in the calling function. For example −

  • This way is perfectly fine
  • Why you store functions in a list at the first place? You can call them whenever you want without them store in a list.
  • That seems fine to me. Of course, you will need to remember the order of the functions in the list. Otherwise, you may want to use a dictionary and access the function objects by name: options = {'func1':func1, 'func2':func2}; options['func1'](). But then you have to ask yourself why you are doing this in the first place. You could just put the functions in a module and import that instead.
  • Thanks, I wasnt sure. Never seen something like this and all I did was just experimenting and guessing. I dont want it to cause future problems in my program cause I dont know if this is safe / actually working etc.
  • @qqvc because this is actually code reducing and easier to modify in my case. I get user input (a number) and now I only have to make sure the index is valid and call the function from that list by index. Otherwise I would have to create a huge if block. That is messy, ugly and a lot of code. You know what I mean?
  • There is not really a reason to create a class if you do not need to maintain state or use any special methods. If you simply need a namespace, use a module.
  • @iCodez ... I agree ... but its easier to show a class for namespace in stack overflow answers