@@ -277,39 +277,74 @@ confusing API to your users.
277277Core Language
278278=============
279279
280- How do you set a global variable in a function?
281- -----------------------------------------------
282-
283- Did you do something like this? ::
284-
285- x = 1 # make a global
286-
287- def f():
288- print x # try to print the global
289- ...
290- for j in range(100):
291- if q > 3:
292- x = 4
293-
294- Any variable assigned in a function is local to that function. unless it is
295- specifically declared global. Since a value is bound to ``x `` as the last
296- statement of the function body, the compiler assumes that ``x `` is
297- local. Consequently the ``print x `` attempts to print an uninitialized local
298- variable and will trigger a ``NameError ``.
299-
300- The solution is to insert an explicit global declaration at the start of the
301- function::
302-
303- def f():
304- global x
305- print x # try to print the global
306- ...
307- for j in range(100):
308- if q > 3:
309- x = 4
310-
311- In this case, all references to ``x `` are interpreted as references to the ``x ``
312- from the module namespace.
280+ Why am I getting an UnboundLocalError when the variable has a value?
281+ --------------------------------------------------------------------
282+
283+ It can be a surprise to get the UnboundLocalError in previously working
284+ code when it is modified by adding an assignment statement somewhere in
285+ the body of a function.
286+
287+ This code:
288+
289+ >>> x = 10
290+ >>> def bar ():
291+ ... print (x)
292+ >>> bar()
293+ 10
294+
295+ works, but this code:
296+
297+ >>> x = 10
298+ >>> def foo ():
299+ ... print (x)
300+ ... x += 1
301+
302+ results in an UnboundLocalError:
303+
304+ >>> foo()
305+ Traceback (most recent call last):
306+ ...
307+ UnboundLocalError: local variable 'x' referenced before assignment
308+
309+ This is because when you make an assignment to a variable in a scope, that
310+ variable becomes local to that scope and shadows any similarly named variable
311+ in the outer scope. Since the last statement in foo assigns a new value to
312+ ``x ``, the compiler recognizes it as a local variable. Consequently when the
313+ earlier ``print x `` attempts to print the uninitialized local variable and
314+ an error results.
315+
316+ In the example above you can access the outer scope variable by declaring it
317+ global:
318+
319+ >>> x = 10
320+ >>> def foobar ():
321+ ... global x
322+ ... print (x)
323+ ... x += 1
324+ >>> foobar()
325+ 10
326+
327+ This explicit declaration is required in order to remind you that (unlike the
328+ superficially analogous situation with class and instance variables) you are
329+ actually modifying the value of the variable in the outer scope:
330+
331+ >>> print (x)
332+ 11
333+
334+ You can do a similar thing in a nested scope using the :keyword: `nonlocal `
335+ keyword:
336+
337+ >>> def foo ():
338+ ... x = 10
339+ ... def bar ():
340+ ... nonlocal x
341+ ... print (x)
342+ ... x += 1
343+ ... bar()
344+ ... print (x)
345+ >>> foo()
346+ 10
347+ 11
313348
314349
315350What are the rules for local and global variables in Python?
0 commit comments