What is closure?
Short answer may be like this: a closure is a combination of code and scope. Python functions are a combination of code to be executed and the scope in which to execute them. However, most of the time when we speak about closure, it's about nested function and the scope of the function. Sometimes we want a function to retain a value when it was created even though the scope cease to exist. This technique of using the values of outer parameters within a dynamic function is another way of defining the closure.
Let's think about the behavior of the function objects in the following code:
def startAt(start): def incrementBy(inc): return start + inc return incrementBy f = startAt(10) g = startAt(100) print f(1), g(2)
Closures in python are created by function calls. In the code above, the call to startAt() creates a binding for start that is referenced inside the function incrementBy(). Each call to startAt() creates a new instance of this function, but each instance has a link to a different binding of start.
If we run it:
So, it looks like the call objects f and g retain their states at the time they were created. When we create f, the outer function startAt() uses the nested function incrementBy() as a return value. Note that it's the function itself that is returned, not the return value of that function. The inner function is not called within the startAt() function. So, the startAt() is a function that returns a function when called. In that way, our program can have an external reference to the nested function, and the nested function retains its reference to the call object of the outer function. In that way, the call object for a particular invocation of the outer function continues to live.
In summary, a closure is a function (object) that remembers its creation environment (enclosing scope).
def startAt(start): def incrementBy(inc): return start + inc return incrementBy closure1 = startAt(10) closure2 = startAt(100) print 'clsure1(3) = %s' %(closure1(3)) print 'closure2(3) = %s' %(closure2(3))
clsure1(3) = 13 closure2(3) = 103
Invoking the variable closure1 (which is of function type) with closure1(3) will return 13, while invoking closure2(3) will return 103. While closure1 and closure2 are both references to the function incrementBy, the associated environment will bind the identifier start to two distinct variables in the two invocations, leading to different results.
We can get more info using __closure__ attribute and cell objects:
def startAt(start): def incrementBy(inc): return start + inc return incrementBy f = startAt(10) g = startAt(100) print 'type(f)=%s' %(type(f)) print 'f.__closure__=%s' %(f.__closure__) print 'type(f.__closure__)=%s' %(type(f.__closure__)) print 'f.__closure__.cell_contents=%s' %(f.__closure__.cell_contents) print 'type(g)=%s' %(type(g)) print 'g.__closure__=%s' %(g.__closure__) print 'type(g.__closure__)=%s' %(type(g.__closure__)) print 'g.__closure__.cell_contents=%s' %(g.__closure__.cell_contents)
From the output below, we can see each cell of the objects retains the value at the time of its creation:
type(f)=<type 'function'> f.__closure__=<cell at 0x7f9a18e8ca60: int object at 0x1a0d170> type(f.__closure__)=<type 'cell'> f.__closure__.cell_contents=10 type(g)=<type 'function'> g.__closure__=<cell at 0x7f9a18e8cbe8: int object at 0x1a0d080> type(g.__closure__)=<type 'cell'> g.__closure__.cell_contents=100
Just want to remind you of the fact: "Many functional languages rely heavily on closures."
Let's switch the name of the inner function:
def startAt(start): def invisible(inc): return start + inc return invisible f = startAt(10) g = startAt(100)
As the new name suggested the name of the nested function is invisible to the outside. So, if that's the case, we can use anonymous lambda function like this:
def startAt(start): return lambda inc: start+inc f = startAt(10) g = startAt(100) print f(1), g(2)
It still gives us the same result!
Running Python Programs (os, sys, import)
Modules and IDLE (Import, Reload, exec)
Object Types - Numbers, Strings, and None
Strings - Escape Sequence, Raw String, and Slicing
Strings - Methods
Formatting Strings - expressions and method calls
Files and os.path
Traversing directories recursively
Regular Expressions with Python
Object Types - Lists
Object Types - Dictionaries and Tuples
Functions def, *args, **kargs
map, filter, and reduce
Sets (union/intersection) and itertools - Jaccard coefficient and shingling to check plagiarism
Hashing (Hash tables and hashlib)
Dictionary Comprehension with zip
The yield keyword
Generator Functions and Expressions
Classes and Instances (__init__, __call__, etc.)
if__name__ == '__main__'
@static method vs class method
Private attributes and private methods
bits, bytes, bitstring, and constBitStream
json.dump(s) and json.load(s)
Python Object Serialization - pickle and json
Python Object Serialization - yaml and json
Priority queue and heap queue data structure
Graph data structure
Dijkstra's shortest path algorithm
Prim's spanning tree algorithm
Functional programming in Python
Remote running a local file using ssh
SQLite 3 - A. Connecting to DB, create/drop table, and insert data into a table
SQLite 3 - B. Selecting, updating and deleting data
MongoDB with PyMongo I - Installing MongoDB ...
Python HTTP Web Services - urllib, httplib2
Web scraping with Selenium for checking domain availability
REST API : Http Requests for Humans with Flask
Blog app with Tornado
Python Network Programming I - Basic Server / Client : A Basics
Python Network Programming I - Basic Server / Client : B File Transfer
Python Network Programming II - Chat Server / Client
Python Network Programming III - Echo Server using socketserver network framework
Python Network Programming IV - Asynchronous Request Handling : ThreadingMixIn and ForkingMixIn
Python Interview Questions I
Python Interview Questions II
Python Interview Questions III
Python Interview Questions IV
Python Interview Questions V
Image processing with Python image library Pillow
Python and C++ with SIP
PyDev with Eclipse
Redis with Python
NumPy array basics A
NumPy Matrix and Linear Algebra
Pandas with NumPy and Matplotlib
Batch gradient descent algorithm
Longest Common Substring Algorithm
Python Unit Test - TDD using unittest.TestCase class
Simple tool - Google page ranking by keywords
Google App Hello World
Google App webapp2 and WSGI
Uploading Google App Hello World
Python 2 vs Python 3
virtualenv and virtualenvwrapper
Uploading a big file to AWS S3 using boto module
Scheduled stopping and starting an AWS instance
Cloudera CDH5 - Scheduled stopping and starting services
Removing Cloud Files - Rackspace API with curl and subprocess
Checking if a process is running/hanging and stop/run a scheduled task on Windows
Apache Spark 1.3 with PySpark (Spark Python API) Shell
Apache Spark 1.2 Streaming
bottle 0.12.7 - Fast and simple WSGI-micro framework for small web-applications ...
Flask app with Apache WSGI on Ubuntu14/CentOS7 ...
Fabric - streamlining the use of SSH for application deployment
Ansible Quick Preview - Setting up web servers with Nginx, configure enviroments, and deploy an App
Neural Networks with backpropagation for XOR using one hidden layer
NLP - NLTK (Natural Language Toolkit) ...
RabbitMQ(Message broker server) and Celery(Task queue) ...
OpenCV3 and Matplotlib ...
Simple tool - Concatenating slides using FFmpeg ...
iPython - Signal Processing with NumPy
iPython and Jupyter - Install Jupyter, iPython Notebook, drawing with Matplotlib, and publishing it to Github
iPython and Jupyter Notebook with Embedded D3.js
Downloading YouTube videos using youtube-dl embedded with Python
Machine Learning : scikit-learn ...
Django 1.6/1.8 Web Framework ...
Ph.D. / Golden Gate Ave, San Francisco / Seoul National Univ / Carnegie Mellon / UC Berkeley / DevOps / Deep Learning / Visualization