I’ve used decorators before and so I was surprised to find a bug in my code:
def make_handler(name, panels):
def get(self):
admin = True
keys = [ndb.Key('Panel', panel) for panel in panels]
panels = zip(ndb.get_multi(keys), panels)
panels = [(panel.panel_html if panel else get_default_content(panel_id), panel_id) for panel, panel_id in panels]
templates = {'panels': panels, 'admin': admin}
self.render_template('panel_page.html', **templates)
return type(name, (BaseHandler,), {'get': get})
The resulting error is:
Traceback (most recent call last):
File "C:\Program Files\Google\google_appengine\lib\webapp2\webapp2.py", line 1536, in __call__
rv = self.handle_exception(request, response, e)
File "C:\Program Files\Google\google_appengine\lib\webapp2\webapp2.py", line 1530, in __call__
rv = self.router.dispatch(request, response)
File "C:\Program Files\Google\google_appengine\lib\webapp2\webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "C:\Program Files\Google\google_appengine\lib\webapp2\webapp2.py", line 1102, in __call__
return handler.dispatch()
File "C:\Program Files\Google\google_appengine\lib\webapp2\webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "C:\Program Files\Google\google_appengine\lib\webapp2\webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "C:\Users\Robert\PycharmProjects\balmoral_doctors\main.py", line 35, in get
keys = [ndb.Key('Panel', panel) for panel in panels]
UnboundLocalError: local variable 'panels' referenced before assignment
My fix is to change panel to panel2 beyond the first usage:
def make_handler(name, panels):
def get(self):
admin = True
keys = [ndb.Key('Panel', panel) for panel in panels]
panels2 = zip(ndb.get_multi(keys), panels)
panels2 = [(panel.panel_html if panel else get_default_content(panel_id), panel_id) for panel, panel_id in panels2]
templates = {'panels': panels2, 'admin': admin}
self.render_template('panel_page.html', **templates)
return type(name, (BaseHandler,), {'get': get})
This is my understanding: panels = zip(...) means that panels is a local variable, so the function doesn’t look in the outer scope for panels.
This is done before the get() function is run, and not midway through?
I thought it would firstly grab panels from the outer function, and then when panels gets defined in the inner function after that, it would from then on use the new local panels variable.
Am I on the right track?
Yes.
Python’s scoping rules indicate that a function defines a new scope level, and a name is bound to a value in only one scope level in a scope level—it is statically scoped (i.e. all scoping is determined at compilation time). As you understood, you’re trying to violate that by reading from a non-local declaration and writing to a local variable. As you observe, the interpreter objects violently to this by raising an
UnboundLocalError: it has understood thatpanelsis a local variable (because it can’t be that and non-local at the same time), but you haven’t assigned (bound) a value to the name, and so it fails.More technical details
The decision was made in Python to keep track of where variables are at compilation time in the bytecode (to be specific for this case, it’s in a tuple
get.__code__.co_varnamesfor local variables), meaning that a variable can be only used in a single scope level in a certain scope. In Python 2.x, it is not possible to modify a non-local variable; you have either read-only access to a global or non-local variable, or read-write access to a global variable by using theglobalstatement, or read-write access to a local variable (the default). That’s just the way it’s been designed (probably for performance and purity). In Python 3, thenonlocalstatement has been introduced with a similar effect toglobal, but for an intermediate scope.Binding the modified variable to a different name is the correct solution in this case.