For example, I have the following list:
method = [fun1, fun2, fun3, fun4]
Then I show a menu where the user must select a number from 1-4 (len(method)). If the user selects i, I have to use the function funi. How can I do that?
Eg.
A=['hello','bye','goodbye']
def hello(n):
print n**2
def bye(n):
print n**3
def goodbye(n):
print n**4
If I want to call the function bye by the array A, using
>>>A[1](7)
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
A[2](5)
TypeError: 'str' object is not callable
How can I use the names saved in A to call my functions? because every method is saved in a string.
funi = method[i-1](indices are zero-based in Python)? Perhaps try reading a Python tutorial on lists?A = [hello, bye, goodbye])?A=[hello, bye, goodbye]instead. In this way, you store the methods, not their names. And then you can use them likeA[1](17)