[issue6782] Scoping of variables in closures

2009-08-25 Thread victor

New submission from victor bitto...@uwp.edu:

I can't tell if this is feature or a bug but either way this
behavior struck me as strange.

def func():
  x = 5
  def inc():
temp = x + 1 # when inc() is execute, this line fails
x = temp
return x
  return inc

i = func()
i() # will raise error

It says that x referenced before defined (when computing x + 1). It
seems that the fact that x is assigned in the local inc() that it
shadows the outer x.
Even stranger is that if you remove the the assignment of x = temp
there is no error; suggesting the assignment to x anywhere in the inner
function shadows the entire function. 

This is not the expected behavior, so I thought it may be bug.

--
components: Interpreter Core
messages: 91954
nosy: bitfort
severity: normal
status: open
title: Scoping of variables in closures
type: behavior
versions: Python 2.6

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue6782
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue6782] Scoping of variables in closures

2009-08-25 Thread R. David Murray

R. David Murray rdmur...@bitdance.com added the comment:

It's feature.

In python 3 you can use the 'nonlocal' keyword to access the variable in
the outer scope for assignment.

--
nosy: +r.david.murray
priority:  - normal
resolution:  - invalid
stage:  - committed/rejected

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue6782
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue6782] Scoping of variables in closures

2009-08-25 Thread Benjamin Peterson

Changes by Benjamin Peterson benja...@python.org:


--
status: open - closed

___
Python tracker rep...@bugs.python.org
http://bugs.python.org/issue6782
___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com