Python Storing Functions in a List - Python Q&A

AI Thread Summary
It is indeed possible to store multiple functions in a list in Python and call them from that list. A user demonstrated this by defining three functions, func1, func2, and func3, and then placing them into a list named A. By iterating over the list and invoking each function, the output confirmed that the functions were successfully called in sequence. This method allows for organized function management and easy execution.
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

 
Technology 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
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Replies
2
Views
1K
Replies
8
Views
1K
Replies
18
Views
1K
Replies
9
Views
3K
Replies
11
Views
1K
Back
Top