mirror of
https://github.com/kennethreitz/python-guide.git
synced 2026-06-05 23:00:18 +00:00
Merge pull request #303 from claudejrogers/patch-1
Update "Late Binding Closures" section of gotchas.rst
This commit is contained in:
@@ -166,6 +166,16 @@ its arguments by using a default arg like so:
|
|||||||
def create_multipliers():
|
def create_multipliers():
|
||||||
return [lambda x, i=i : i * x for i in range(5)]
|
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
|
When the Gotcha Isn't a Gotcha
|
||||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user