mirror of
https://github.com/kennethreitz/python-guide.git
synced 2026-06-05 23:00:18 +00:00
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:
@@ -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
|
||||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
|
||||
|
||||
Reference in New Issue
Block a user