Why is the use of an undefined name not a syntax error?

Devin Jeanpierre jeanpierreda at gmail.com
Sun Apr 1 17:40:22 EDT 2018


> But if it is cheap to detect a wide variety of name errors at compile time, is there any particular reason it is not done?

>From my perspective, it is done, but by tools that give better output
than Python's parser. :)

Linters (like pylint) are better than syntax errors here, because they
collect all of the undefined variables, not just the first one. Maybe
Python could/should be changed to give more detailed errors of this
kind as well. e.g. Clang parse errors for C and C++ are much more
thorough and will report all of your typos, not just the first one.

> P.S. Here are some uncommon language features that interfere with identifying all valid names. In their absence, one might expect an invalid name to be a syntax error:

Also, if statements, depending on what you mean by "invalid":

def foo(x):
  if x:
    y = 3
  return y # will raise UnboundLocalError if not x

-- Devin



More information about the Python-list mailing list