0

I have the following function in python:

def foo():
    print 1 
    return 1

In the shell, I run

foo()

and I get

1
1

as I should. But when I run the following in the shell

exec('foo')

I get nothing? why?

This is a diluted version of a much larger problem.

1 Answer 1

1

You are only referencing the function name. Add parenthesis to actually call the function:

exec('foo()')

This will print 1; the return value is discarded as nothing captures it. You could add an extra print statement to show the return value:

exec('print foo()')

exec() does not mean 'execute the function named', it means 'execute the python code given'.

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

Comments

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.