3

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.

5
  • 1
    Do you mean funi = method[i-1] (indices are zero-based in Python)? Perhaps try reading a Python tutorial on lists? Commented May 31, 2015 at 21:09
  • yes, I just need the way to call the method, the index is easy to fix, thanks ! Commented May 31, 2015 at 21:21
  • Edited for understand Commented May 31, 2015 at 21:37
  • 1
    Why are you storing strings in the list, rather than the functions themselves (A = [hello, bye, goodbye])? Commented May 31, 2015 at 21:38
  • 1
    Use A=[hello, bye, goodbye] instead. In this way, you store the methods, not their names. And then you can use them like A[1](17) Commented May 31, 2015 at 21:39

2 Answers 2

8

Let's see...

You call a function fn by using parens () as in fn()

You access an item from a list by the indexing operator [] as in lst[idx]

So, by combining the two lst[idx](*args)

EDIT

Python list indices are zero-based, so for the first item is 0, the second 1, etc... If the user selects 1-4, you'll have to subtract one.

EDIT

Given the clarification, the following can be done

def method1():
    pass

def method2():
    pass


methods = [method1, method2]

And you can use the logic above. This way you won't have to mess with actual resolving the string of a name of the function to the actual function.

Keep in mind that functions in python are first class so you can store a reference of them to a list (what we do in methods=[] line)

Sign up to request clarification or add additional context in comments.

7 Comments

@RodrigoLópez I'm thinking in 0-based indexing :)
In question he selects from 1 to 4
I just gave an example, exactly I'm using: method=['LU','CHOLESKY','Gauss', 'Gaussnopivot', 'Gaussconpivot', 'Gaussconviotparcial', 'Gaussconpivotcomp']
Where have you defined the methods ?
@LuisFelipeVillavicencioLopez they're strings, not methods. Are they the names of methods?
|
2

In your list are not functions but strings (which have "randomly" the same name as the functions). Just put the functions inside the list:

def hello():
    pass

mylist = [hello]
mylist[0]()

If you need the name of a function you can use a.__name__ e.g.

def hello():
    pass
mylist = [hello]
print("Available functions:")
for function in mylist:
    print(function.__name__)

4 Comments

But I need to use the name of the functions because I print a Menu where every method is enumerate, and I call the method by their enumeration.
Use myfunc.__name__ to get the name of a function as a string.
e.g print myfun.(hello) will print 'hello' ?
If your function is called hello print(hello.__name__) will print "hello"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.