I am getting a NameError that is not defined and not sure why it happen - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: I am getting a NameError that is not defined and not sure why it happen (/thread-33971.html) |
I am getting a NameError that is not defined and not sure why it happen - rick0922 - Jun-14-2021 This is my Code: def test(): exec("a=100") print(a) test()The Traceback i get is this: It does not work.However, exec("a=100") print(a)It works, why? In my situation, I need to put it in the function. RE: I am getting a NameError that is not defined and not sure why it happen - Larz60+ - Jun-14-2021 1st you should avid using exec It does work: >>> def test(): ... exec("a=100") ... print(a) ... >>> test() 100 >>>The proper way: >>> def test(): ... a = 100 ... print(a) ... >>> test() 100 print(a) RE: I am getting a NameError that is not defined and not sure why it happen - rick0922 - Jun-14-2021 (Jun-14-2021, 01:01 PM)Larz60+ Wrote: 1st you should avid using exec >>> def test(): ... exec("a=100") ... print(a) ... >>> test() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 3, in test NameError: name 'a' is not definedWhich version are you using? I am using 3.9.5 and it does not work. RE: I am getting a NameError that is not defined and not sure why it happen - deanhystad - Jun-14-2021 I get a name error when running the program. Maybe a difference between Windows and Linux? exec() is obviously creating a new scope and "a" has not been defined outside that scope. This works: def test(): exec("global a; a=100") print(a) test() And as expected, this does not.a = 42 def test(): exec("a=100") print(a) test()
RE: I am getting a NameError that is not defined and not sure why it happen - rick0922 - Jun-14-2021 (Jun-14-2021, 01:22 PM)deanhystad Wrote: I get a name error when running the program. Maybe a difference between Windows and Linux? exec() is obviously creating a new scope and "a" has not been defined outside that scope. Thank you very much. It is very useful. It helps me a lot. RE: I am getting a NameError that is not defined and not sure why it happen - deanhystad - Jun-14-2021 This is interesting. def test(): a = 0 exec("print(dir())") test() exec is "inheriting" variables defined in the local scope, but these must not be the same variables. Changing the variable "inside" exec does not change the value "outside" exec.def test(): a = 0 exec("print(a);a=5;print(a)") print(a) test() This is similar to what happens when using fork. In the Unix/Linux world the forked process shares the same variables as the parent process. Changing "a" in the forked process changes "a" in the parent because a is the same variable, resides in the same memory. In Windows a brand new process is created for the fork and the parent process and forked process are separate. The forked process has the same variable names and initial values, but the forked variables are not the same variables that were defined in the parent process.
|