Reputation: 313
I have to call the function in a for loop. All those functions are stored in a list as a string with quotes. I need to remove those quotes and store the values again in a list.
What needs to be done:
Python
fruits = ['apple','mango','orange']
print(type(fruits))
func = '[%s]'%','.join(map(str,fruits))
print(func) ## [apple,mango,orange]
print(type(func))
def apple():
print("In apple")
def mango():
print("In mango")
def orange():
print("In orange")
n = len(func)
func_it = itertools.cycle(func)
for i in range(n):
next(func_it)()
Output
<class 'list'>
[apple,mango,orange]
<class 'str'>
After removing the quotes from the strings, Its data type is getting changed to . Is there any way to remove quotes from a list of strings and store those values as a list again?
Upvotes: 1
Views: 2200
Reputation: 1
Here, the line hehe = eval(func)
creates a list of the names which are later taken as function calls and it's possible because the ending parentheses "()" are not necessary, atleast in Python 3.9.
import itertools
def apple():
print("In apple")
def mango():
print("In mango")
def orange():
print("In orange")
fruits = ['apple','mango','orange']
print(type(fruits))
func = '[%s]'%','.join(map(str,fruits))
print(func) ## [apple,mango,orange]
hehe = eval(func)
print(type(hehe))
n = len(hehe)
func_it = itertools.cycle(hehe)
for i in range(n):
next(func_it)()
output:
<class 'list'>
[apple,mango,orange]
<class 'list'>
In apple
In mango
In orange
Upvotes: 0
Reputation: 352
You can use the built in python exec() function that will execute any string as code.
#!/usr/bin/env python3
fruits = ['apple','mango','orange']
def apple():
print("In apple")
def mango():
print("In mango")
def orange():
print("In orange")
for func in fruits:
exec(func + '()')
Output
In apple
In mango
In orange
Upvotes: 1
Reputation: 9061
You can use globals()
to get the function object using name then you can use that object
func = [globals()[fun] for fun in fruits]
func_it = itertools.cycle(func)
for i in range(len(func)):
next(func_it)()
Output:
In apple
In mango
In orange
Upvotes: 1
Reputation: 5889
Based on your code i'm guessing you want to call a function based on a string? I suggest using a dictionary
import itertools
fruits = ['apple','mango','orange']
def apple():
print("In apple")
def mango():
print("In mango")
def orange():
print("In orange")
funcs = {'apple':apple()}
funcs['apple']
out
In apple
Upvotes: 1
Reputation: 21275
You can't call functions like that. Removing quotes from a string won't turn it into a function. You are constructing func
to be '[apple, mango, orange]'
which is a string. When you iterate over that you get each of the characters of the string. i.e you get [
, a
etc. Each is a string & you can't call strings. You're basically doing '['()
which is meaningless.
Remember - in Python - functions are first-class objects. If you want to list over functions just put references to those functions in a list:
import itertools
def apple():
print("In apple")
def mango():
print("In mango")
def orange():
print("In orange")
func = [apple, mango, orange] # list of functions
n = len(func)
func_it = itertools.cycle(func)
for i in range(n):
x = next(func_it)
print(type(x)) # check the type
x()
Which results in:
<class 'function'>
In apple
<class 'function'>
In mango
<class 'function'>
In orange
So if you want to construct this list from your string '[apple, mango, orange]'
you need to eval
it:
s = '[apple, mango, orange]'
func = eval(s)
print(func)
Which results in:
[<function apple at 0x000001FB9E7CF040>, <function mango at 0x000001FB9ECB7940>, <function orange at 0x000001FB9ECB7DC0>]
However if possible you should always try to avoid using eval
Upvotes: 2