I'm trying to understand Python's exec() function.
So I have a string which is a function like code = """def somefn(x): return x + x y = somefn(z)"""
and I am trying to run this in another function like:
def otherfn(codestr): z = 2 exec(codestr) return y
otherfn(code)
but it gives me the error: Traceback (most recent call last): File "C:/Users/Admin/Desktop/heh.py", line 11, in otherfn(code) File "C:/Users/Admin/Desktop/heh.py", line 9, in otherfn return y NameError: name 'y' is not defined
it works fine outside the function like: exec(codestr) print(y)
it finds y just fine but not sure why it is bugging out when it is in the function.
How can I fix this? Is it something to do with globals() and locals()? Using Python 3.6 btw.
i want to return the output from the function which is the y = y = somefn(z) when i pass in z.
Ayden Harris
what are you trying to understand?
Oliver Bennett
why i can get y back when it's outside the function but not inside. or rather how i can embed exec into a function and retrieve a variable from it.
Dominic Morris
I think you're trying to understand something a little over your head, you didnt even post your code in a legible code block
Bentley Torres
i get that, i'm a brainlet i'll admit it. but can you help me with this?
Charles Fisher
I have a 4 year degree in CS and I literally cannot understand what this mess of code is supposed to be doing, let alone what you are attempting to teach yourself, post a question about something specific, is it your errors?
Eli Martin
You're initializing z after y. Also name your shit so you get less confused.
def doubler(val): return val + val y = doubler
print(y(2)) // 4
def quadrupler(val): return y(val) + y(val)
print(quadrupler(6)) // 24
Adrian Miller
do you know what an exec is actually even doing? why are you trying to do this? there are way better ways of passing functions etc like this, rather than execing entire processes
Michael Butler
misreply
Chase Foster
Yes it is the error, it cannot find y when i use exec inside this other function. but it can outside of it with: z = 2 exec(codestr) print(y)
what are you trying to do? what is this snippet of code supposed to do?
Parker Scott
I am writing a GP algorithm which needs to test the functions it creates, I am doing a simple input and output type function. How would you go about this?
Logan Hall
there is so little context to your question it doesn't make sense
Robert Gutierrez
So my program uses a genetic algorithm to write functions and I have that working just fine. The problem is when I need to evaluate the function and I don't know how to best go about that with Python 3.6.
Parker Gutierrez
>I am writing a GP algorithm which needs to test the functions it creates write a function that tests these functions, you don't need to exec a function to test it........
Lucas Gutierrez
Sorry I'm just trying to understand why 2 works and 1 doesn't and how I can get 1 to work like 2 does. I need to evaluate the output of a function from a given input.
I want to retrieve the variable y from the string code which has a function when I'm in otherfn().
How do I go about that? I write the function out as a string which needs to take in an input to produce an output for evaluation. How would you do this?
Christopher Perry
why are you returning y in 1?
Henry Sanchez
because I need to evaluate the output against what it should be producing for the cost function, I wrote the code so it does y = somefn(z). Is there a workaround for this?
Aiden Sanchez
OK maybe to simplify my question: why does 2 work and 1 doesn't?
And more importantly how can I get y from the exec when it is in a function?
>And more importantly how can I get y from the exec when it is in a function static and globals most likely what you're gonna need, I'm not a python programmer but a C derivative would use something as such,
with that, why are you attempting to exec the operation of assigning a value to a variable? why not simply define y globally and assign it without using an exec, there literally is no reason you should be using it if you are simply evaluating the output of a function...
Jordan Mitchell
So what you are doing doesn't really make sense, but the reason why "2" works and "1" doesn't is because of scope. The docs explain that exec() has optional arguments to supply a dictionary of global variables or both global and local variables. If no dictionaries are passed to exec() then it operates inside the current scope docs.python.org/3/library/functions.html#exec
In 2, exec() is being called globally and so it has access to all global variables. In this case, variables z and y are also being assigned globally. x is a local variable, but it is being created and used only inside exec() so there is no issue.
In 1, exec() is being called inside otherfn() and so by default only has access to the local variables inside otherfn(). You have to call exec() with a dictionary containing the global variables you want to be able to access. Luckily Python has the function globals() which returns such a dictionary.
Here's the tricky part. In the docs it's explained local variables will not be affected unless a "locals" dictionary is supplied to exec() explicitly. Global variables do not have this limitation.
This is why 2 works but 1 fails. changing the code so that exec is called like exec(codestr, globals(), locals())
Will stop the error from being thrown because exec() now has access to global and local variables, but when I tested the code on my local machine it did not output the expected results. For whatever reason "y" was being assigned the literal value of 4 and I could not change this.
In any case, your code is horrible and whatever you're trying to do, this is not the way to do it. Code like exec() should never need to be called.
I think what you want to do is in here. It tells you the error y is not defined, because it isn't in that process. However, by the looks of it you, the value returned in the code passed into exec() is returned.
So, something like: return exec('//code')
Matthew Hill
The function is being written by the genetic algorithm as a string over and over. So I need a way to take it from a string and run it.
Adrian Ward
I still get the same error when I try that. How would you go about what I'm trying to do?
Sorry. Restarting my program I see that this doesn't work. However if you get rid of "locals()" you'll get a new error "z is no defined".
locals() and globals() don't seem to be providing access to all the necessary variables but the way this is programmed I don't think any of this is possible.
Chase Cruz
>The function is being written by the genetic algorithm as a string over and over. So I need a way to take it from a string and run it. why does this need to be done on the fly?
Kayden Brown
OK I think I got it. Thanks so much /g. I appreciate the help. It did have something to do with globals and locals. Here is the working code btw.
because he doesn't understand how machine learning actually works and wants to reinvent the wheel because he thinks he's a fucking prodigy which will eventually result in something that won't work, if he finishes it at all,.
Grayson Thomas
and another retard is gonna go to sleep thinking he's brilliant. Gotta love Jow Forums