Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 51a727d

Browse filesBrowse 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
Copy full SHA for 51a727d

File tree

Expand file treeCollapse file tree

1 file changed

+10
-0
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+10
-0
lines changed

‎docs/writing/gotchas.rst

Copy file name to clipboardExpand all lines: docs/writing/gotchas.rst
+10Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.