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

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -