Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Bugs

[issue7276] UnboundLocalError scoping problem with nested functions

 

 

Python bugs RSS feed   Index | Next | Previous | View Threaded


report at bugs

Nov 6, 2009, 11:23 AM

Post #1 of 4 (319 views)
Permalink
[issue7276] UnboundLocalError scoping problem with nested functions

New submission from Ole Laursen <olau [at] iola>:

This works:

def outer(name):
tmp = name
def inner():
print(tmp)
return inner

outer("foo") # prints "foo"

While the same code with one extra line (setting tmp after printing)
fails

def outer(name):
tmp = name
def inner():
print(tmp)
tmp = "hello"
return inner

outer("foo")()

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "<stdin>", line 4, in inner
UnboundLocalError: local variable 'tmp' referenced before assignment

and the following works

def outer(name):
tmp = name
def inner():
tmp = "hello"
print(tmp)
return inner

outer("foo")() # prints "hello"

Now, I understand there's an interesting issue of assignment binding to
the inner-most scope. So tmp = "hello" is binding a new variable, not
changing the outermost tmp. But I should still be able to read tmp,
right? It looks like some kind of optimizer error.


For the record, this pattern came up in a decorator like this

def deco(x = None):
def inner(fn):
if not x:
x = somedefaultvalue

return inner

which gives the same UnboundLocalError error.

----------
messages: 94994
nosy: olau
severity: normal
status: open
title: UnboundLocalError scoping problem with nested functions
type: compile error

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7276>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Nov 6, 2009, 1:21 PM

Post #2 of 4 (278 views)
Permalink
[issue7276] UnboundLocalError scoping problem with nested functions [In reply to]

Benjamin Peterson <benjamin [at] python> added the comment:

Any assignment causes a value to be local to its current scope.

----------
nosy: +benjamin.peterson
resolution: -> wont fix
status: open -> closed

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7276>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Nov 9, 2009, 3:39 AM

Post #3 of 4 (268 views)
Permalink
[issue7276] UnboundLocalError scoping problem with nested functions [In reply to]

Ole Laursen <olau [at] iola> added the comment:

OK, sorry, I was under the impression that the global binding was still
available (I can't find anything to the contrary here
http://docs.python.org/reference/simple_stmts.html#assignment-statements
) but it's obviously using a static definition of scope.

The error message isn't super helpful, though. :) Would it make sense to
add a "(non-local "tmp" is shadowed)"? I guess that's easy to detect?

----------

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7276>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com


report at bugs

Nov 9, 2009, 11:15 AM

Post #4 of 4 (268 views)
Permalink
[issue7276] UnboundLocalError scoping problem with nested functions [In reply to]

R. David Murray <rdmurray [at] bitdance> added the comment:

The relevant section is
http://docs.python.org/reference/executionmodel.html#naming-and-binding.
Perhaps a cross-reference should be added to the assignment section.
If you think it should, please open a new issue for that.

I have opened issue 7290 with a suggested improvement to the FAQ entry
that most closely deals with this. Could you review that and let me
know in that issue if you think it is a worthwhile improvement?

----------
nosy: +r.david.murray
priority: -> low
stage: -> committed/rejected
type: compile error -> behavior

_______________________________________
Python tracker <report [at] bugs>
<http://bugs.python.org/issue7276>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: http://mail.python.org/mailman/options/python-bugs-list/list-python-bugs%40lists.gossamer-threads.com

Python bugs RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.