Created
April 22, 2017 21:45
-
-
Save RenSys/e5e8d6322a4c49683f7f3a36be1538f8 to your computer and use it in GitHub Desktop.
Standard - Closures functions return functions
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"metadata": {}, | |
"cell_type": "markdown", | |
"source": "# Closures: Functions that return functions\nA closure is any dynamically-generated function returned by another function. The key property is that the returned function has access to the variables in the local namespace where it was created." | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": false | |
}, | |
"cell_type": "code", | |
"source": "def make_watcher():\n local_call_memory = {}\n\n def has_been_seen(x):\n print('Check local_call_memory for x={}'.format(x))\n if x in local_call_memory:\n print('x={} was alread stored in local_call_memory for'.format(x))\n return True\n else:\n print('x={} is new, store value'.format(x))\n local_call_memory[x] = True\n return False\n\n print('Bind Function: has_been_seen')\n return has_been_seen", | |
"execution_count": 32, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": false | |
}, | |
"cell_type": "code", | |
"source": "watcher = make_watcher()", | |
"execution_count": 33, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Bind Function: has_been_seen\n", | |
"name": "stdout" | |
} | |
] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": true | |
}, | |
"cell_type": "code", | |
"source": "vals = [5, 6, 1, 5, 1, 6, 3, 5]", | |
"execution_count": 34, | |
"outputs": [] | |
}, | |
{ | |
"metadata": { | |
"trusted": true, | |
"collapsed": false | |
}, | |
"cell_type": "code", | |
"source": "[watcher(x) for x in vals]", | |
"execution_count": 35, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"text": "Check local_call_memory for x=5\nx=5 is new, store value\nCheck local_call_memory for x=6\nx=6 is new, store value\nCheck local_call_memory for x=1\nx=1 is new, store value\nCheck local_call_memory for x=5\nx=5 was alread stored in local_call_memory for\nCheck local_call_memory for x=1\nx=1 was alread stored in local_call_memory for\nCheck local_call_memory for x=6\nx=6 was alread stored in local_call_memory for\nCheck local_call_memory for x=3\nx=3 is new, store value\nCheck local_call_memory for x=5\nx=5 was alread stored in local_call_memory for\n", | |
"name": "stdout" | |
}, | |
{ | |
"output_type": "execute_result", | |
"data": { | |
"text/plain": "[False, False, False, True, True, True, False, True]" | |
}, | |
"metadata": {}, | |
"execution_count": 35 | |
} | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"name": "python2", | |
"display_name": "Python 2", | |
"language": "python" | |
}, | |
"language_info": { | |
"mimetype": "text/x-python", | |
"nbconvert_exporter": "python", | |
"name": "python", | |
"pygments_lexer": "ipython2", | |
"version": "2.7.13", | |
"file_extension": ".py", | |
"codemirror_mode": { | |
"version": 2, | |
"name": "ipython" | |
} | |
}, | |
"gist": { | |
"id": "", | |
"data": { | |
"description": "Standard - Closures functions return functions", | |
"public": true | |
} | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment