Added discussion of closures being used for decorators and the usefulness of decorators.

This commit is contained in:
Donne Martin 2015-01-27 11:14:36 -05:00
parent 2de3d867c4
commit 54b1c29bcc

View File

@ -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",
"```"
]
},
{