Unlocking Python Closures: Why They Matter and How to Use Them
This article explains the concept of Python closures, why they are useful for capturing external variables, provides clear code examples, discusses real‑world analogies, and shows how closures are essential in decorators and other advanced patterns.
Concept of Closures
We try to understand closures from a conceptual standpoint.
In some languages, when a function defines another nested function that references variables from the outer function, a closure may be created. A closure can associate a function with a set of “private” variables, allowing those variables to persist across multiple calls of the function. — Wikipedia
In plain words, when a function is returned as an object together with external variables, a closure is formed. Example:
def make_printer(msg):
def printer():
print msg # uses external variable
return printer
printer = make_printer('Foo!')
printer()Languages that treat functions as objects generally support closures, e.g., Python, JavaScript.
Understanding Closures
Why do closures exist? They carry external variables ("private cargo"). If a function does not carry such variables, it is just an ordinary function. The same function with different captured variables can achieve different functionality, similar to lightweight interface encapsulation.
Interfaces define a set of method signature constraints.
def tag(tag_name):
def add_tag(content):
return "<{0}>{1}</{0}>".format(tag_name, content)
return add_tag
content = 'Hello'
add_tag = tag('a')
print add_tag(content) # <a>Hello</a>
add_tag = tag('b')
print add_tag(content) # <b>Hello</b>Real‑world analogies: a phone call uses the phone’s antenna, a restaurant meal uses hidden ingredients; the service is the closure, the hidden parts are the captured variables.
A class instance can be seen as a closure where constructor parameters are the packaged variables, but a class offers more than a single callable.
When to Use Closures
Closures appear frequently in Python, especially when writing decorators with parameters. A decorator must return a callable that accepts a function and returns a function.
# how to define
def wrapper(func1):
# must accept one function as argument
return func2 # returns a callable
# how to use
def target_func(args):
pass
result = wrapper(target_func)(args)
# using @ syntax (equivalent)
@wrapper
def target_func(args):
pass
result = target_func()If a decorator needs its own arguments, an extra outer layer creates a closure that captures those arguments.
def html_tags(tag_name):
def wrapper_(func):
def wrapper(*args, **kwargs):
content = func(*args, **kwargs)
return "<{tag}>{content}</{tag}>".format(tag=tag_name, content=content)
return wrapper
return wrapper_
@html_tags('b')
def hello(name='Toby'):
return 'Hello {}!'.format(name)
print hello() # <b>Hello Toby!</b>
print hello('world') # <b>Hello world!</b>Diving Deeper
Closures have an extra __closure__ attribute containing a tuple of cell objects, each storing a captured external variable.
>> def make_printer(msg1, msg2):
... def printer():
... print msg1, msg2
... return printer
>>> printer = make_printer('Foo', 'Bar')
>>> printer.__closure__
(<cell at 0x...: str object at 0x...>, <cell at 0x...: str object at 0x...>)
>>> printer.__closure__[0].cell_contents
'Foo'
>>> printer.__closure__[1].cell_contents
'Bar'The principle is simple.
References
https://www.the5fire.com/closure-in-python.html
http://stackoverflow.com/questions/4020419/why-arent-python-nested-functions-called-closures
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
