[issue4246] execution model - clear and complete example in documentation

Cherniavsky Beni report at bugs.python.org
Sat Nov 20 22:24:19 CET 2010

Cherniavsky Beni <cben at users.sf.net> added the comment:

The FAQ for this was much improved in 2009 (issue 7290): http://docs.python.org/py3k/faq/programming.html#why-am-i-getting-an-unboundlocalerror-when-the-variable-has-a-value

To support the claim that this keeps biting people, at least the following bug reports all came from people misundestanding this:
issue 10043
issue 9049
issue 7747
issue 7276
issue 6833
issue 5763
issue 4109 (understood effect of =, surprised by +=)
issue 972467
issue 850110
issue 463640
These are just the people who were persistent enough to open a bug (and in most cases managed to produce a minimal example); many more ask on c.l.p, StackOverflow (>50 hits for UnboundLocalError, many of which are this exact issue) etc., or just give up.

[Interesting point: people mostly complain when the unbound reference occurs textually *before* the assignment (though there's selection bias here), and many of them complain that things happen "out of order".  It seems half the misunderstanding is that people expect variables to *become* localized when first assigned - they don't understand it's a static decision affecting all occurences in a function.]

The central problem I believe is that when people try to modify a non-local var and get
  UnboundLocalError: local variable foo referenced before assignment
their mental model of Python scopes is *wrong*, so the error message is *useless* for them (what 'local variable foo' is it talking about?), and have no idea where to look next.

Also, I'm afraid people just won't internalize this issue until it bites them at least once (I've seen a Python course where I had explained this, with a print-before-assignment example, and 2 days later a student was bitten by the exception and was at a loss.  Therefore, I think providing a clear learning path from UnboundLocalError is crucial.


I propose (i.e. am working on patch(s)) attacking it at many points:

(1) Expand UnboundLocalError message to point to help('NAMESPACES') 
    [Execution Model → Naming and Binding] and/or the FAQ.
    (requirement IMHO: use a help() ref that can be followed the 
    from a Python prompt on an offline machine; not sure if FAQ can
    work this way.)

    (1B) Ideally, detect if the var is bound in outer/global scope 
         and suggest help('nonlocal') / help('global') approriately.

    (1C) Ideally, improve UnboundLocalError to explain "foo is local 
         throughout the function because it's bound at line 42".

(2) Add an example to Naming and Binding section.
    Currently, it's a bunch of legalese, defining 7 terms(!) before 
    you get to the meat.  Mortal users won't survive that.
    Being the language spec, the precise legalese must stay there;
    but it'd be good to prepend it with a human-readable blurb and 
    example regarding this issue.

(3) Improve the tutorial.  Sections 4.6 [Defining functions] and 9.2 
    [Scopes and Namespaces] are relevant.  4.6 mentions the issue of 
    assignment to global but neither section has a clear example.
    And 9.2 is scary IMHO; I'll see if I can make it any better...
(4) Add examples to documentation of global & nonlocal?
    Not clear if helpful, probably too much.

nosy: +cben

Python tracker <report at bugs.python.org>

More information about the Python-bugs-list mailing list