From 5da8f3e0e62b5163ae64e6ea38ae2116e6eed0d5 Mon Sep 17 00:00:00 2001 From: Simon Weber Date: Wed, 3 Apr 2013 16:42:36 -0400 Subject: [PATCH] gotchas: fix naming; remove generator in def example --- docs/writing/gotchas.rst | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/docs/writing/gotchas.rst b/docs/writing/gotchas.rst index 1fa23e563..abd166b10 100644 --- a/docs/writing/gotchas.rst +++ b/docs/writing/gotchas.rst @@ -142,28 +142,32 @@ fact the same exact behavior is exhibited by just using an ordinary ``def``: .. code-block:: python - def create_adders(): + def create_multipliers(): + multipliers = [] + for i in range(5): - def adder(x): + def multiplier(x): return i * x - yield adder + multipliers.append(multiplier) + + return multipliers What You Should Do Instead ~~~~~~~~~~~~~~~~~~~~~~~~~~ -Well. Here the general solution is arguably a bit of a hack. Due to Python's +The most general solution is arguably a bit of a hack. Due to Python's afformentioned behavior concerning evaluating default arguments to functions (see :ref:`default_args`), you can create a closure that binds immediately to its arguments by using a default arg like so: .. code-block:: python - def create_adders(): + def create_multipliers(): return [lambda x, i=i : i * x for i in range(5)] When the Gotcha Isn't a Gotcha ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -When you want your closures to behave this way. Late binding is good in lots of +Sometimes you want your closures to behave this way. Late binding is good in lots of situations. Looping to create unique functions is unfortunately a case where they can cause hiccups.