+ 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

9th Jun 2018, 11:13 PM
Ahri Fox
Ahri Fox - avatar
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.
10th Jun 2018, 12:44 AM
Ahri Fox
Ahri Fox - avatar
+ 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
10th Jun 2018, 12:13 AM
Tashi N
Tashi N - avatar
+ 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
10th Jun 2018, 12:46 AM
Tashi N
Tashi N - avatar
+ 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.
10th Jun 2018, 12:09 AM
Tashi N
Tashi N - avatar
+ 9
Pegasus check the code, locals don't work in functions https://code.sololearn.com/c4fAlxoJYEi3/?ref=app
10th Jun 2018, 1:04 AM
Ahri Fox
Ahri Fox - avatar
+ 8
👍
14th Jun 2018, 1:19 AM
Shreya Jain
Shreya Jain - avatar
+ 7
Just played around with it
10th Jun 2018, 1:05 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 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.
14th Jun 2018, 6:01 AM
Ahri Fox
Ahri Fox - avatar
+ 5
Tashi N In python 3.x its not possible because motivations writed in your posted link... Only in python 2.x... Anyway i think that passing an dict and reference it in exec code its sufficient for Ahri Fox needs
10th Jun 2018, 12:56 AM
KrOW
KrOW - avatar
+ 5
I thought exec() works for local variables as well?
10th Jun 2018, 12:56 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 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!
9th Jun 2018, 11:47 PM
Ahri Fox
Ahri Fox - avatar
+ 4
Dmitriy no offense but, that's MY awnser. he copied my code from the question description
13th Jun 2018, 7:13 AM
Ahri Fox
Ahri Fox - avatar
+ 4
just pray around it and enjoy.
14th Jun 2018, 8:15 AM
Madhusudan Saini
Madhusudan Saini - avatar
+ 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.
14th Jun 2018, 5:26 AM
Ahri Fox
Ahri Fox - avatar
+ 3
I like python but I need some complex questions
14th Jun 2018, 6:37 AM
Maria habib
Maria habib - avatar
+ 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.
13th Jun 2018, 3:27 PM
Deepak Mittal
Deepak Mittal - avatar
+ 2
compile() is the key!
13th Jun 2018, 4:11 PM
Siddharth
Siddharth - avatar
+ 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.
13th Jun 2018, 10:43 PM
Ahri Fox
Ahri Fox - avatar
+ 2
14th Jun 2018, 6:10 AM
program
program - avatar
+ 2
😊
14th Jun 2018, 9:30 AM
Maria habib
Maria habib - avatar