From 9ed9bc5e1153d434b217a328287252984f4f3987 Mon Sep 17 00:00:00 2001 From: Gregor Best Date: Mon, 11 May 2015 23:38:02 +0200 Subject: [PATCH] Add documentation for the (nonlocal ...) directive Signed-off-by: Gregor Best --- docs/language/api.rst | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/docs/language/api.rst b/docs/language/api.rst index bbc2868..9a285d5 100644 --- a/docs/language/api.rst +++ b/docs/language/api.rst @@ -1008,6 +1008,29 @@ conditional expression. Some examples: [0, 2, 4, 6, 8] +nonlocal +-------- + +``nonlocal`` can be used to mark a symbol as not local to the current scope. +The parameters are the names of symbols to mark as nonlocal. This is necessary +to modify variables through nested ``let`` scopes: + +.. code-block:: clj + + (let [[x 0]] + (for [y (range 10)] + (let [[z (inc y)]] + (nonlocal x) ; allow the setv to "jump scope" to resolve x + (setv x (+ x y)))) + x) + +Without the call to ``(nonlocal x)``, this code would result in an +UnboundLocalError being raised during the call to ``setv``. This is the result +of the way Python handles scoping for nested functions, which are used by Hy to +implement ``let``. See `PEP3104 `_ +for further information. + + not ---