Update "Late Binding Closures" section of gotchas.rst

The late binding closure example seems like a good use case for the
functools.partial function.
This commit is contained in:
claudejrogers
2013-08-11 20:35:30 -07:00
parent a0f6fc1c16
commit 51a727d079
+10
View File
@@ -166,6 +166,16 @@ its arguments by using a default arg like so:
def create_multipliers():
return [lambda x, i=i : i * x for i in range(5)]
Alternatively, you can use the functools.partial function:
.. code-block:: python
from functools import partial
from operator import mul
def create_multipliers():
return [partial(mul, i) for i in range(5)]
When the Gotcha Isn't a Gotcha
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~