Python Forum
I am getting a NameError that is not defined and not sure why it happen
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
I am getting a NameError that is not defined and not sure why it happen
#1
This is my Code:
def test():
    exec("a=100")
    print(a)
    
test()
The Traceback i get is this:
Error:
Traceback (most recent call last): File "C:\Users\lab135\Desktop\test.py", line 5, in <module> test() File "C:\Users\lab135\Desktop\test.py", line 3, in test print(a) NameError: name 'a' is not defined
It does not work.
However,
exec("a=100")
print(a)
It works, why?
In my situation, I need to put it in the function.
Reply
#2
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)
Reply
#3
(Jun-14-2021, 01:01 PM)Larz60+ Wrote: 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)


>>> 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 defined
Which version are you using? I am using 3.9.5 and it does not work.
Reply
#4
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()
Output:
100
And as expected, this does not.
a = 42

def test():
    exec("a=100")
    print(a)

test()
Output:
42
rick0922 likes this post
Reply
#5
(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.

This works:
def test():
    exec("global a; a=100")
    print(a)

test()
Output:
100
And as expected, this does not.
a = 42

def test():
    exec("a=100")
    print(a)

test()
Output:
42

Thank you very much. It is very useful. It helps me a lot.
Reply
#6
This is interesting.
def test():
    a = 0
    exec("print(dir())")

test()
Output:
['a']
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()
Output:
0 5 0
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  NameError: name 'u1' is not defined (on parser code Python) Melcu54 1 153 Jul-26-2021, 04:36 PM
Last Post: snippsat
  [split] NameError: name 'start_with_first_selected' is not defined knoxvilles_joker 4 604 Apr-18-2021, 07:59 PM
Last Post: knoxvilles_joker
  “NameError” “is not defined” script that I know works on other computers iamwithstupid 0 587 Nov-24-2020, 04:23 PM
Last Post: iamwithstupid
  NameError: name 'L2' is not defined soshelby 5 996 Nov-05-2020, 08:50 PM
Last Post: soshelby
  NameError: name 'target_id' is not defined bhagyashree 7 910 Nov-03-2020, 10:22 AM
Last Post: bhagyashree
  code not working, NameError: name 's' is not defined ridgerunnersjw 4 1,039 Oct-05-2020, 07:03 PM
Last Post: buran
  Going thru tutorials..."NameError: name 'raw_input' is not defined" hmonnier 4 1,165 Jul-14-2020, 02:19 PM
Last Post: BitPythoner
  NameError: name 'pins' is not defined glennford49 2 1,623 Jun-15-2020, 12:20 PM
Last Post: glennford49
  python library not defined in user defined function johnEmScott 2 867 May-30-2020, 04:14 AM
Last Post: DT2000
  [split] NameError: name 'formats' is not defined shobhu123 1 1,097 May-24-2020, 06:29 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020