0

I am currently making a module which exports the local variables to a python readable file and imports it through exec, but I am unable to modify them without using returns (Which is the whole idea). The problem boils down to:

I have two files, a module (module.py) and a main (main.py). I want the module to be able to execute code inside the main:

#module.py
def foo():
    exec("b = a")
#main.py
from module import foo
a = 10
foo()
print(b)

Expected outcome

10

Actual outcome

NameError: name 'b' is not defined
martineau
  • 119,623
  • 25
  • 170
  • 301
hhhhhhhhhn
  • 31
  • 3
  • 3
    Why would you want to do this? – cdarke Jul 15 '19 at 15:40
  • 1
    Not only is this almost certainly a bad way of doing whatever it is you're trying to do, but it will also be very difficult: https://stackoverflow.com/a/15959638/5946921 – Will Da Silva Jul 15 '19 at 15:46
  • 1
    If you are using `exec` or `eval` you are probably doing something wrong. Their are a few cases these are useful such as in a few programming usecases where you want a dynamic function with string formatting BUT they expose a security vulnerability in your code and should almost always be avoided. – Error - Syntactical Remorse Jul 15 '19 at 15:46

1 Answers1

-1

You can use __main__

main.py:

from module import foo
import __main__
a = 10
__main__ = foo(__main__)
print(b)

module.py:

def foo(main):
    main.b = main.a
    return main
bapap
  • 514
  • 8
  • 25
  • If you're going to assign b to something by calling `foo`, and you're going to make `foo` take in `a`, why not just have `foo` return `a`? What purpose does `yield` serve here? It seems like using it along with a for loop only server to complicate things. – Will Da Silva Jul 15 '19 at 15:50
  • You can use return too but you can use yield. Yield is like return – bapap Jul 15 '19 at 15:53
  • I understand what `yield` is. I'm asking why you're using it here instead of `return`. – Will Da Silva Jul 15 '19 at 15:54
  • Because it is doing the similar work with ```return```. I thought ```return``` is easy and ```yield``` is a bit harder and used it because of this. – bapap Jul 15 '19 at 16:09
  • Why would you intentionally obfuscate your code? In any case, they do different things. `return` causes a function to exit, and returns a value. Having `yield` inside a function definition results in the function returning a generator, which when iterated over will provide a value each time `yield` is invoked. Your answer does not answer the question. If you remove the pointless yield, then you've effectively turned `foo` into an identity function. This question is asking how to alter a value in the global context of a different module (where the caller of the function is located). – Will Da Silva Jul 15 '19 at 16:14
  • Ok. I will change to return. – bapap Jul 15 '19 at 16:18
  • That's better, but still doesn't answer the question. You've made `foo` into an identity function (i.e. it doesn't do anything). – Will Da Silva Jul 15 '19 at 16:19
  • I have made it with ```__main__``` – bapap Jul 15 '19 at 16:26