Mar-20-2020, 02:49 PM
SyntaxError
is different than all other error,it will fail upon the first syntax error.This will shut down parser at a early stage and parsing to first stage(byte code) never happens.
So here is there two
SyntaxError
and a NameError
,but as mention will it stop at first error SyntaxError
and shut down.So no more action is possible before fix this error.
def foo(: a = bar # NameError ages = { 'pam': 24, 'jim': 24 # SyntaxError 'michael': 43 } print(ages) foo()
Output:λ python error_test.py
File "error_test.py", line 1
def foo(:
^
SyntaxError: invalid syntax
If fix the first line SyntaxError
.If will now not find the
NameError
first,as SyntaxError
trump all other error and as mention shout down the parser.All other errors need the parser to be finish before they are reported.
So now it jump to second
SyntaxError
.def foo(): a = bar # NameError ages = { 'pam': 24, 'jim': 24 # SyntaxError 'michael': 43 } print(ages) foo()
Output:λ python error_test.py
File "error_test.py", line 6
'michael': 43
^
SyntaxError: invalid syntax
An as often answered here when people post that they have SyntaxError
,look at line before.If fix
SyntaxError
now will the NameError
get reported.def foo(): a = bar # NameError ages = { 'pam': 24, 'jim': 24, # Fixed 'michael': 43 } print(ages) foo()
Output:λ python error_test.py
Traceback (most recent call last):
File "error_test.py", line 10, in <module>
foo()
File "error_test.py", line 2, in foo
a = bar # NameError
NameError: name 'bar' is not defined