Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Improve UnboundLocalError message.
Raymond Hettinger reported during PyCon Canada 2016 Keynote (~20m30 sec)
That unbound local error message was inaccurate, it state that :

> local variable 'xxx' referenced before assignment

Though it can be assigned and deleted.

    for a toy example:

       def foo():
          x = 1
          del x
          print(x)

       foo()

Do the same for free variable.

    def multiplier(n):
	def multiply(x):
	    return x * n
	del n
	return multiply
  • Loading branch information
Carreau committed Feb 16, 2017
commit c8f9cbca28f4f903eb44023210d08a065dfcd530
4 changes: 2 additions & 2 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,10 @@ static PyObject * special_lookup(PyObject *, _Py_Identifier *);
#define NAME_ERROR_MSG \
"name '%.200s' is not defined"
#define UNBOUNDLOCAL_ERROR_MSG \
"local variable '%.200s' referenced before assignment"
"local variable '%.200s' referenced before assignment, or got deleted"
#define UNBOUNDFREE_ERROR_MSG \
"free variable '%.200s' referenced before assignment" \
" in enclosing scope"
" in enclosing scope, or got deleted"

/* Dynamic execution profile */
#ifdef DYNAMIC_EXECUTION_PROFILE
Expand Down