52

For testing purposes I want to directly execute a function defined inside of another function.

I can get to the code object of the child function, through the code (func_code) of the parent function, but when I exec it, i get no return value.

Is there a way to get the return value from the exec'ed code?

user2433423
  • 521
  • 1
  • 4
  • 3
  • I don't think you can do this with `exec`. You'll need to do something like [georg's answer](http://stackoverflow.com/a/23918011/27358) or use `new` as described in [this answer](http://stackoverflow.com/a/1235801/27358). – David Moles May 28 '14 at 17:27
  • @DavidMoles: `new` is deprecated, but `types` are fine, thanks. – gog May 28 '14 at 17:34

11 Answers11

40

Yes, you need to have the assignment within the exec statement:

>>> def foo():
...     return 5
...
>>> exec("a = foo()")
>>> a
5

This probably isn't relevant for your case since its being used in controlled testing, but be careful with using exec with user defined input.

wnnmaw
  • 5,444
  • 3
  • 38
  • 63
  • That is so weird, Especially that you dont need to declare the variable before hand. – marsh Jan 06 '15 at 14:22
  • This isn't working for me. `exec('ds_id = kim_processors.%s.process(%s)' % (function.python_module, model_id))` does populate ds_id, as I can see in the Django locals debug output. However I then run `cursor.execute("""select e.id as ... where dataset_id=%s""", (ds_id))` and get error "Exception Type: NameError Exception Value: name 'ds_id' is not defined". Yet locals shows ds_id is indeed set (to 14). Is there some restriction on its scope? – Chris Jan 23 '15 at 13:09
  • Also tried `exec('import kim_processors.%s as processor_module' % function.python_module)` then running `ds_id = processor_module.process(model_id)`. Again, I can see `processor_module` listed in localvars, but I get the same NameError exception "Exception Value: name 'processor_module' is not defined" – Chris Jan 23 '15 at 13:39
  • 2
    @Chris, I'm not sure what the issue is, but it sounds like a good question for the site. Try posting it separately as a question and refer here to show what you've tried. – wnnmaw Jan 23 '15 at 15:30
  • 1
    Thanks @wnnmaw that's exactly what I did! Sorry forgot to follow up on this thread. The question and answers are [here](http://stackoverflow.com/questions/28111527/access-a-variable-instantiated-within-a-python-exec-command-works-but-throws) There is a better way of importing (importlib) and the duplicate question describes the scope issue beautifully. Thanks for responding. – Chris Jan 23 '15 at 16:01
  • 1
    This doesn't work, exec needs to be launched with globals, exec("a = foo()", globals()) – misantroop Feb 16 '20 at 00:17
  • @misantroop, just tested on Python 3.7.0 and 3.8.1, works fine on both. What system are you using where it doesn't work? – wnnmaw Feb 18 '20 at 13:17
  • @wnnmaw this doesn't work in general only when a is a global variable. You can see it not working by simply moving your code into a `def foo(): ... exec('a =='` statement. May I suggest updating your answer to show that it only works when the variable you modify is a global? eg. In a function you need to, eg. `def foo(): exec('global a\na = 5')`. – Doug Aug 21 '20 at 08:00
38

A few years later, but the following snippet helped me:

the_code = '''
a = 1
b = 2
return_me = a + b
'''

loc = {}
exec(the_code, globals(), loc)
return_workaround = loc['return_me']
print(return_workaround)  # 3

exec() doesn't return anything itself, but you can pass a dict which has all the local variables stored in it after execution. By accessing it you have a something like a return.

I hope it helps someone.

Mr. B.
  • 8,041
  • 14
  • 67
  • 117
  • 1
    This is good. But you could just use this code and not have a seperate dictionary for "loc": exec(the_code) return_workaround = locals()['return_me'] – Patrick McGloin Apr 06 '21 at 10:09
  • This works, but inside the code I am unable to reference to variables that where defined outside it. – bomben Sep 25 '21 at 19:43
  • @bomben I didn't test it, but can't you pass your outer variables? It should be mentioned in the docs of `exec()`. :) – Mr. B. Sep 26 '21 at 07:36
11

While this is the ugliest beast ever seen by mankind, this is how you can do it by using a global variable inside your exec call:

def my_exec(code):
    exec('global i; i = %s' % code)
    global i
    return i

This is misusing global variables to get your data across the border.

>>> my_exec('1 + 2')
3

Needless to say that you should never allow any user inputs for the input of this function in there, as it poses an extreme security risk.

devsnd
  • 7,382
  • 3
  • 42
  • 50
  • Thank you! +1. I struggled a lot with getting a reference to a module given a string and this is what works. You can use this by giving the string as parameter code and it will return the actual module. – Chris Fowl May 07 '19 at 13:28
8

use eval() instead of exec(), it returns result

ex10se
  • 89
  • 1
  • 1
4

Something like this can work:

def outer():
    def inner(i):
        return i + 10


for f in outer.func_code.co_consts:
    if getattr(f, 'co_name', None) == 'inner':

        inner = type(outer)(f, globals())

        # can also use `types` module for readability:
        # inner = types.FunctionType(f, globals())

        print inner(42) # 52

The idea is to extract the code object from the inner function and create a new function based on it.

Additional work is required when an inner function can contain free variables. You'll have to extract them as well and pass to the function constructor in the last argument (closure).

gog
  • 10,367
  • 2
  • 24
  • 38
3

Here's a way to return a value from exec'd code:

def exec_and_return(expression):
    exec(f"""locals()['temp'] = {expression}""")
    return locals()['temp']

I'd advise you to give an example of the problem you're trying to solve. Because I would only ever use this as a last resort.

Menzies
  • 112
  • 1
  • 5
3

This doesn't get the return value per say, but you can provide an empty dictionary when calling exec to retrieve any variables defined in the code.

# Python 3
ex_locals = {}
exec("a = 'Hello world!'", None, ex_locals)
print(ex_locals['a'])
# Output: Hello world!

From the Python 3 documentation on exec:

The default locals act as described for function locals() below: modifications to the default locals dictionary should not be attempted. Pass an explicit locals dictionary if you need to see effects of the code on locals after function exec() returns.

For more information, see How does exec work with locals?

Stevoisiak
  • 23,794
  • 27
  • 122
  • 225
1

Here's a solution with a simple code:

# -*- coding: utf-8 -*-
import math

x = [0]
exec("x[0] = 3*2")
print(x[0]) # 6
Josef
  • 2,869
  • 2
  • 22
  • 23
Namwon Kim
  • 11
  • 1
  • 3
  • Similar answers are already there posted. Better to look at new questions to answer where community needs you. – Ank May 29 '21 at 10:23
  • 1
    @Ank You are so right. But, I also want to share a simple method. Thank you. – Namwon Kim May 29 '21 at 11:55
1

Since Python 3.7, dictionary are ordered. So you no longer need to agree on a name, you can just say "last item that got created":

>>> d = {}
>>> exec("def addone(i): return i + 1", d, d)
>>> list(d)
['__builtins__', 'addone']
>>> thefunction = d[list(d)[-1]]
>>> thefunction
<function addone at 0x7fd03123fe50>
Mathieu Longtin
  • 15,922
  • 6
  • 30
  • 40
-1

if we need a function that is in a file in another directory, eg
we need the function1 in file my_py_file.py
located in /home/.../another_directory
we can use the following code:

def cl_import_function(a_func,py_file,in_Dir):
... import sys
... sys.path.insert(0, in_Dir)
... ax='from %s import %s'%(py_file,a_func)
... loc={}
... exec(ax, globals(), loc)
... getFx = loc[afunc]
... return getFx

test = cl_import_function('function1',r'my_py_file',r'/home/.../another_directory/')

test()
(a simple way for newbies...)

  • Whilst your answer may be useful, the question was asked 6 years ago and already has 5 answers, although none have been accepted. – GoodJuJu Dec 12 '20 at 22:05
-2
program = 'a = 5\nb=10\nprint("Sum =", a + b)'

program = exec(program)

print(program)
NameVergessen
  • 598
  • 8
  • 26
  • 2
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – NameVergessen Dec 07 '22 at 01:13