[docs] [issue30023] Example code becomes invalid for "Why do lambdas defined in a loop with different values all return the same result?"

Philip Lee report at bugs.python.org
Sun Apr 9 02:16:55 EDT 2017


New submission from Philip Lee:

There example code here becomes invalid 
https://docs.python.org/3/faq/programming.html#why-do-lambdas-defined-in-a-loop-with-different-values-all-return-the-same-result
>>> squares = []
>>> for x in range(5):
	squares.append(lambda: x**2)

	
>>> squares
[<function <lambda> at 0x01FB7A08>, <function <lambda> at 0x01F82390>, <function <lambda> at 0x01FBA3D8>, <function <lambda> at 0x01FBA420>, <function <lambda> at 0x01FBA468>]
>>> 

There returned value is a List of lambda functions, not numbers

----------
assignee: docs at python
components: Documentation
messages: 291353
nosy: docs at python, iMath
priority: normal
severity: normal
status: open
title: Example code becomes invalid for "Why do lambdas defined in a loop with different values all return the same result?"
type: enhancement
versions: Python 3.6

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue30023>
_______________________________________


More information about the docs mailing list