+ 33
exec() doesn't work with local variables in function [Python]
I need exec() to modify a local variable. I read in somewhere and in documentation that including locals() would help it but it doesn't. So I'm kinda stumped! Check out my code to see what I mean. https://code.sololearn.com/c4fAlxoJYEi3/?ref=app
33 Réponses
+ 26
Tashi N
def test3():
d=1
ldic=locals()
exec("d+=1",globals(),ldic)
d=ldic["d"]
print(d) # it works! returns 2
test3()
#ah thanks Tashi! The chosen awnser on stack overflow worked. however I needed to have the variable before hand unlike the example, but that's what I want anyway.
+ 24
Yes, works for
global b
b=1
I'm quite sure there is a way to pass the scope to the exec function alternatively.
But I guess that's exactly the task: you want it local.
Phew, found it Ahri Fox
https://stackoverflow.com/questions/1463306/how-does-exec-work-with-locals
+ 10
Ahri Fox Great, was about to post this because I saw it didn't work for you in first place, so I add this anyways ;)
https://code.sololearn.com/c9RnKkPL2F44/?ref=app
+ 9
Wasn't it something with passing the scope in the method arguments?
globals or something?
Argh, can't remember. I'll search for it.
+ 9
Pegasus check the code, locals don't work in functions
https://code.sololearn.com/c4fAlxoJYEi3/?ref=app
+ 8
👍
+ 7
Just played around with it
+ 6
Zohir Yep, that output is proof.
First one is 2, which means it's working correctly, since exec() is adding 1 to variable a, which is 1.
Then comes the problem, I do the same code in a function, and it outputs 1, meaning it didn't add anything.
I try a recommended solution in another function, and it doesn't work, as evidenced by the 1.
Then I do Tashi's solution linked in a stack overflow link, and it works! it adds 1 which results in 2 inside a function.
So yeah, your original statement that it works for you is FALSE considering that output.
+ 5
I thought exec() works for local variables as well?
+ 4
Ace
def test3():
d=1
codeobj=compile("d+=1",'sumstring','exec')
exec(codeobj)
print(d)
test3()
#this returns 1. But I might be using compile() it wrong!
+ 4
Dmitriy
no offense but, that's MY awnser. he copied my code from the question description
+ 4
just pray around it and enjoy.
+ 3
Joseph David Zamora Murillo
eval() wouldn't have helped in my situation (which isn't mentioned in this thread). I needed to execute code; not evaluate code. eval can't set variables. I needed to set/modify variables.
+ 3
I like python but I need some complex questions
+ 2
Dear Programmer,
exec() function works well in all conditions in Java for RunTime Environment.
But not sure about Python work.
Good Luck for best future.
+ 2
compile() is the key!
+ 2
Zohir this code is proof. try it on any machine. I've verified through code playground, another online REPL, and my own machine
https://code.sololearn.com/c4fAlxoJYEi3/?ref=app
Of course, the third test works correctly since it was the suggested awnser in this thread, which I added after the fact.
+ 2
Ahri Fox ok
+ 2
😊