Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Update "Late Binding Closures" section of gotchas.rst #303

Merged
merged 1 commit into from

2 participants

@claudejrogers

The late binding closure example seems like a good use case for the
functools.partial function.

@claudejrogers claudejrogers Update "Late Binding Closures" section of gotchas.rst
The late binding closure example seems like a good use case for the
functools.partial function.
51a727d
@kennethreitz

thanks!

@kennethreitz kennethreitz merged commit 2029979 into from
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 12, 2013
  1. @claudejrogers

    Update "Late Binding Closures" section of gotchas.rst

    claudejrogers authored
    The late binding closure example seems like a good use case for the
    functools.partial function.
This page is out of date. Refresh to see the latest.
Showing with 10 additions and 0 deletions.
  1. +10 −0 docs/writing/gotchas.rst
View
10 docs/writing/gotchas.rst
@@ -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
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Something went wrong with that request. Please try again.