Storing Functions in a List - Python Q&A

Join the discussion
Registration is free. Ask a follow-up in this thread, or start your own.
1 reply · 2K views
zeion
Messages
455
Reaction score
1

Homework Statement



Hello,

I just have a small question with Python.

Is it possible to store multiple functions in a list? So I can call them from the list?

Like if I had :

def func1():
...

def func2():
...

def func3():
...

Can I put them in a list and call them from the list?

Thanks.

Homework Equations





The Attempt at a Solution

 
Physics news on Phys.org
Hey zeion,

Try this:

Code:
>>> def func1():
...     print "1"
...
>>> def func2():
...     print "2"
...
>>> def func3():
...     print "3"
...
>>> A = [func1, func2, func3]
>>>
>>> for a in A:
...     a()
...
1
2
3

- Warren