1

I have two blocks of python code, one works, and the other doesn't.


Working block:

env = {'user':'xyz'}
for key, value in env.items():
    exec("{} = value".format(key))

print(user)

output:

xyz


The block, which doesn't work:

def test():
    env = {'user':'xyz'}
    for key, value in env.items():
        exec("{} = value".format(key))

    print(user)

test()

output:

NameError: name 'user' is not defined


The only difference I see is that the first block is being called in the global scope.

Could you please explain?

Many thanks!

PS: With all due respect, I know, I should avoid using exec() but what if I want to.

5
  • 1
    The global scope is just a dictionary, and can be modified by exec or various other means. But the local scope of a function is carved in stone at the time the function was compiled (all references get turned into an index into an array), there's no way to add names at runtime. Commented Feb 8, 2021 at 2:35
  • @jasonharper thanks, if I print locals(), it confirms the presence of this variable:{'env': {'user': 'xyz'}, 'key': 'user', 'value': 'xyz', 'user': 'xyz'}. What are your thoughts? Commented Feb 8, 2021 at 2:40
  • As said above, it shows in locals() and also in the debugger, it shows up as a variable. Why does that happen? i.imgur.com/Ry1xoDX.png When I try to access it on the next line, it raises exception. Commented Feb 8, 2021 at 2:42
  • if you really need to use exec() then use it - but I would rather keep it as dictionary env['user'] = ... because it can be more useful then creating variables user = ... Commented Feb 8, 2021 at 3:33
  • @furas the whole point of discussion is that I am not able to use exec() Commented Feb 8, 2021 at 4:14

1 Answer 1

1

I recommend you reading this

You have to use locals() or exec or eval to access variables defined by exec in a function in Python3.

def test():
    env = {'user': 'xyz'}
    for key, value in env.items():
        exec("{} = value".format(key))
    exec("print(user)")
    print(locals()['user'])
    print(eval("user"))
 
test()

It should be noted that, if you attempt to store value returned from eval. You will get NameError.

def test():
    env = {'user': 'xyz'}
    for key, value in env.items():
        exec("{} = value".format(key))
    user = eval("user")
    print(user)

test()

returns

Traceback (most recent call last):
  File "D:/Git/sscgc/test.py", line 8, in <module>
    test()
  File "D:/Git/sscgc/test.py", line 5, in test
    user = eval("user")
  File "<string>", line 1, in <module>
NameError: name 'user' is not defined
Sign up to request clarification or add additional context in comments.

1 Comment

I had similar findings!! Thanks, @david190810 for sharing the article. It was helpful

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.