From 51a727d0791de0afc6627da1bda02b6b59498918 Mon Sep 17 00:00:00 2001 From: claudejrogers Date: Sun, 11 Aug 2013 20:35:30 -0700 Subject: [PATCH] Update "Late Binding Closures" section of gotchas.rst The late binding closure example seems like a good use case for the functools.partial function. --- docs/writing/gotchas.rst | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/docs/writing/gotchas.rst b/docs/writing/gotchas.rst index 98bfd2c47..12e18619e 100644 --- a/docs/writing/gotchas.rst +++ b/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 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~