[issue13094] Need Programming FAQ entry for the behavior of closures

2013-01-04 Thread Ezio Melotti

Ezio Melotti added the comment:

Attached a new patch.

--
Added file: http://bugs.python.org/file28563/issue13094-2.diff

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



[issue13094] Need Programming FAQ entry for the behavior of closures

2013-01-04 Thread R. David Murray

R. David Murray added the comment:

Looks good to me.

--

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



[issue13094] Need Programming FAQ entry for the behavior of closures

2013-01-04 Thread Roundup Robot

Roundup Robot added the comment:

New changeset fdc894d44d82 by Ezio Melotti in branch '2.7':
#13094: add Programming FAQ entry about the behavior of closures.
http://hg.python.org/cpython/rev/fdc894d44d82

New changeset 02933454b7ce by Ezio Melotti in branch '3.2':
#13094: add Programming FAQ entry about the behavior of closures.
http://hg.python.org/cpython/rev/02933454b7ce

New changeset 827ddaaa45e4 by Ezio Melotti in branch '3.3':
#13094: merge with 3.2.
http://hg.python.org/cpython/rev/827ddaaa45e4

New changeset 1bf7ae6c5324 by Ezio Melotti in branch 'default':
#13094: merge with 3.3.
http://hg.python.org/cpython/rev/1bf7ae6c5324

--
nosy: +python-dev

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



[issue13094] Need Programming FAQ entry for the behavior of closures

2013-01-04 Thread Ezio Melotti

Ezio Melotti added the comment:

Fixed, thanks for the review!

--
resolution:  - fixed
stage: patch review - committed/rejected
status: open - closed

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



[issue13094] Need Programming FAQ entry for the behavior of closures

2013-01-03 Thread R. David Murray

R. David Murray added the comment:

The FAQ (as in, this question gets asked again and again) is something like 
why do the lambdas I define in a loop all return the same result when the 
input value was different when each one was defined?

The same applies to regular functions, but people almost never do that in a 
loop, so in that case they are more likely to think of the scoping issue.

--

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



[issue13094] Need Programming FAQ entry for the behavior of closures

2013-01-03 Thread Ezio Melotti

Ezio Melotti added the comment:

 why do the lambdas I define in a loop all return the same result when
 the input value was different when each one was defined?

I thought about that, but that sounds a bit too long/specific.  It also has the 
problem that the issue is not strictly related to lambdas or loops (even if 
this combination might be more common), and doesn't say where the result come 
from.

--

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



[issue13094] Need Programming FAQ entry for the behavior of closures

2013-01-03 Thread R. David Murray

R. David Murray added the comment:

The point is, it is a FAQ.  We are talking about updating the FAQ document.  It 
doesn't matter if the text is too specific, if it is in fact a FAQ.  And it 
is.

--

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



[issue13094] Need Programming FAQ entry for the behavior of closures

2013-01-03 Thread Ezio Melotti

Ezio Melotti added the comment:

Here's a patch.

--
keywords: +patch
stage: needs patch - patch review
Added file: http://bugs.python.org/file28550/issue13094.diff

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



[issue13094] Need Programming FAQ entry for the behavior of closures

2013-01-03 Thread Ezio Melotti

Changes by Ezio Melotti ezio.melo...@gmail.com:


--
assignee:  - ezio.melotti

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



[issue13094] Need Programming FAQ entry for the behavior of closures

2013-01-02 Thread Ezio Melotti

Ezio Melotti added the comment:

I'm having some problem at deciding what the title of the FAQ should be, and 
what the actual problem is.  ISTM that OP's problem is the same as:
 x = 1
 def foo(): return x
... 
 x = 2
 foo()
2

except that he has 3 lambdas in a loop that get attached to an instance rather 
than a simple function -- but the problem is that in both cases the function 
references a global variable whose value is retrieved at calling time rather 
that being set at definition time.
IOW the solution should be clear, but the code is complex enough that it's not 
easy to recognize the analogy with the simpler case.
I'm not even sure this has anything to do with closures, unless you consider 
the global scope a closure.

Maybe the What are the rules for local and global variables in Python? FAQ 
could be expanded with a few examples to cover this case too.

--
assignee: ezio.melotti - 
nosy: +chris.jerdonek

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



[issue13094] Need Programming FAQ entry for the behavior of closures

2012-11-28 Thread Ezio Melotti

Changes by Ezio Melotti ezio.melo...@gmail.com:


--
assignee: docs@python - ezio.melotti

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



[issue13094] Need Programming FAQ entry for the behavior of closures

2012-11-18 Thread Ezio Melotti

Changes by Ezio Melotti ezio.melo...@gmail.com:


--
keywords: +easy
stage:  - needs patch
type:  - enhancement
versions: +Python 3.4

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



[issue13094] Need Programming FAQ entry for the behavior of closures

2011-10-09 Thread Éric Araujo

Changes by Éric Araujo mer...@netwok.org:


--
assignee:  - docs@python
components: +Documentation -None
nosy: +eric.araujo
versions: +Python 3.2, Python 3.3

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



[issue13094] Need Programming FAQ entry for the behavior of closures

2011-10-03 Thread R. David Murray

Changes by R. David Murray rdmur...@bitdance.com:


--
title: setattr misbehaves when used with lambdas inside for loop - Need 
Programming FAQ entry for the behavior of closures

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



[issue13094] Need Programming FAQ entry for the behavior of closures

2011-10-03 Thread Ezio Melotti

Ezio Melotti ezio.melo...@gmail.com added the comment:

To understand better what's going on, try to change the value of 'each' after 
the 3 prints and then call again the 3 methods: you will see that they now 
return the new value of each.  This is because the lambdas refer to global 
'each' (that at the end of the loop is set to 'baz').
If you do setattr(x, each, lambda each=each: each), the each will be local to 
the lambda, and it will then work as expected.

An entry in the FAQ would be useful, I thought it was there already but 
apparently it's not (I'm pretty sure I saw this already somewhere in the doc, 
but I can't seem to find where).

--
assignee: docs@python - 
components: +None -Documentation
nosy: +ezio.melotti
stage: needs patch - 
type: behavior - 
versions:  -Python 3.2, Python 3.3

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



[issue13094] Need Programming FAQ entry for the behavior of closures

2011-10-03 Thread Tomáš Dvořák

Tomáš Dvořák dvto...@gmail.com added the comment:

Thank you all very much for the super-quick responses. I'm used to smalltalk, 
so the python variable binding behaviour is unnatural to me, but I guess there 
must have been some reasons for making it behave this way. 

Ezio, the 
lambda each=each: each 
trick works nicely, thanks a lot. But - what does it mean? :) I just don't know 
how to parse and understand it :-)

Best regards,
  Tomáš Dvořák

--

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



[issue13094] Need Programming FAQ entry for the behavior of closures

2011-10-03 Thread Ezio Melotti

Ezio Melotti ezio.melo...@gmail.com added the comment:

Maybe with a different name is less confusing: lambda return_value=each: 
return_value
This copies the value of 'each' in a variable called 'return_value' that is 
local to the lambda.  Since the copy happens when the lambdas are defined, 
'return_value' has then the right value.

--

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