I need to call a function whose name is available in a string in python
eg:
[(x, y) for x in [1,2,3] for y in [3,1,4] if x != y]
I need to execute the above statement but the variable count may increase according to user input any idea?
Another Example :
a = "for y in range(2,20):print y"
I need to execute the above string as a python command
Actual Problem is no of loops and variable may increase depends upon the input
somtimes i wnat to iterate over 3 item so 3 loops sometime 4 item then 4 loops
so i figured out this way if i generate string with the variables and if i can execute it problem can be solved
The built-in
evalfunction is what you are looking for:If you want variables, you can use the
localsargument:If you want to execute statements instead of evaluating expressions, use
exec: