This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: odd behavior in creating list of lambda expressions
Type: behavior Stage: resolved
Components: Versions:
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: John Sahr, abarry
Priority: normal Keywords:

Created on 2016-08-11 15:15 by John Sahr, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (3)
msg272454 - (view) Author: John Sahr (John Sahr) Date: 2016-08-11 15:15
The following produces unexpected behavior.
I think that it should produce a list of six different lambda expressions,
but after creation, all six lambda expressions produce the same output.
It's possible that I'm missing something about Python.

##### begin example #######
from math import *

mm = []

for n in range(6):
    f = lambda x: sin(n*x)
    print f, f(1.0)
    mm.append(f)
    
print '***'

for m in mm:
    print m, m(1.0)
###### end example ####
msg272455 - (view) Author: Anilyka Barry (abarry) * (Python triager) Date: 2016-08-11 15:17
This is due to the fact that Python evaluates the variable 'n' when the function is called, not when it is created. As such, the variable holds the latest value for all functions, and they exhibit identical behaviour.

Workaround:

...
f = lambda x, n=n: sin(n*x)
...

And this should work as you expect. More information is available at https://docs.python.org/3/faq/programming.html#why-do-lambdas-defined-in-a-loop-with-different-values-all-return-the-same-result
msg272541 - (view) Author: John Sahr (John Sahr) Date: 2016-08-12 14:32
I eventually figured that out that source of the problem;

thanks for the coding fix; that's useful to know.

-John

On Thu, Aug 11, 2016 at 8:17 AM, Emanuel Barry <report@bugs.python.org>
wrote:

>
> Emanuel Barry added the comment:
>
> This is due to the fact that Python evaluates the variable 'n' when the
> function is called, not when it is created. As such, the variable holds the
> latest value for all functions, and they exhibit identical behaviour.
>
> Workaround:
>
> ...
> f = lambda x, n=n: sin(n*x)
> ...
>
> And this should work as you expect. More information is available at
> https://docs.python.org/3/faq/programming.html#why-do-
> lambdas-defined-in-a-loop-with-different-values-all-return-the-same-result
>
> ----------
> nosy: +ebarry
> resolution:  -> not a bug
> stage:  -> resolved
> status: open -> closed
> versions:  -Python 2.7
>
> _______________________________________
> Python tracker <report@bugs.python.org>
> <http://bugs.python.org/issue27738>
> _______________________________________
>
History
Date User Action Args
2022-04-11 14:58:34adminsetgithub: 71925
2016-08-12 14:32:01John Sahrsetmessages: + msg272541
2016-08-11 15:17:50abarrysetstatus: open -> closed

versions: - Python 2.7
nosy: + abarry

messages: + msg272455
resolution: not a bug
stage: resolved
2016-08-11 15:15:32John Sahrcreate