From 54b1c29bcc0fd33e8b6dcd604dd34705a6fdddda Mon Sep 17 00:00:00 2001 From: Donne Martin Date: Tue, 27 Jan 2015 11:14:36 -0500 Subject: [PATCH] Added discussion of closures being used for decorators and the usefulness of decorators. --- core/functions.ipynb | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/core/functions.ipynb b/core/functions.ipynb index f5fae6a..0a2ce3c 100644 --- a/core/functions.ipynb +++ b/core/functions.ipynb @@ -1,7 +1,7 @@ { "metadata": { "name": "", - "signature": "sha256:d9028f29469f49a645af74dd1ee28614caee8ca7ca47089925560fa874a2384b" + "signature": "sha256:1123ef3475d906de39c34c42c18ebeda8c0f1c60ee01b6e692125a51b325d8e7" }, "nbformat": 3, "nbformat_minor": 0, @@ -226,7 +226,17 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "Closures are dynamically-genearated functions returned by another function. The returned function has access to the variables in the local namespace where it was created." + "Closures are dynamically-genearated functions returned by another function. The returned function has access to the variables in the local namespace where it was created. \n", + "\n", + "Closures are often used to implement decorators. Decorators are useful to transparently wrap something with additional functionality:\n", + "\n", + "```python\n", + "def my_decorator(fun):\n", + " def myfun(*params, **kwparams):\n", + " do_something()\n", + " fun(*params, **kwparams)\n", + " return myfun\n", + "```" ] }, {