python - Why doesn't my function execute when I use exec on a string? (the string is the name of the function) -
i have following function in python:
def foo(): print 1 return 1 in shell, run
foo() and
1 1 as should. when run following in shell
exec('foo') i nothing? why?
this diluted version of larger problem.
you referencing function name. add parenthesis call function:
exec('foo()') this print 1; return value discarded nothing captures it. add print statement show return value:
exec('print foo()') exec() not mean 'execute function named', means 'execute python code given'.
Comments
Post a Comment