Skip to content

Commit 51a727d

Browse files
committed
Update "Late Binding Closures" section of gotchas.rst
The late binding closure example seems like a good use case for the functools.partial function.
1 parent a0f6fc1 commit 51a727d

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

docs/writing/gotchas.rst

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,16 @@ its arguments by using a default arg like so:
166166
def create_multipliers():
167167
return [lambda x, i=i : i * x for i in range(5)]
168168
169+
Alternatively, you can use the functools.partial function:
170+
171+
.. code-block:: python
172+
173+
from functools import partial
174+
from operator import mul
175+
176+
def create_multipliers():
177+
return [partial(mul, i) for i in range(5)]
178+
169179
When the Gotcha Isn't a Gotcha
170180
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
171181

0 commit comments

Comments
 (0)