search
HomeBackend DevelopmentPython TutorialTalk about closures in Python - Closure

Closure in Python is not a concept that can be understood immediately, but as you learn more, you need to understand such a thing no matter what.

The concept of closure

Let’s try to understand closure conceptually.

In some languages, when another function can be (nested) defined in a function, if the inner function refers to the variables of the outer function, a closure may occur. Closures can be used to create an association between a function and a set of "private" variables. These private variables maintain their persistence across multiple calls to a given function.

——Wikipedia)

In easier-to-understand terms, when a function is returned as an object, external variables are entrained, forming a closure. See examples.

def make_printer(msg): 
    def printer(): 
        print msg  # 夹带私货(外部变量) 
    return printer  # 返回的是函数,带私货的函数 
 
printer = make_printer('Foo!') 
printer()

Programming languages ​​that support using functions as objects generally support closures. Such as Python, JavaScript.

How to understand closure

What is the significance of closure? Why is closure needed?

I personally think that the meaning of closure is that it carries external variables (private goods). If it does not carry private goods, It is no different from an ordinary function. The same function carries different private goods to achieve different functions. In fact, you can also understand it this way. The concept of closure is very similar to that of interface-oriented programming. You can understand closure as a lightweight interface encapsulation.

The interface defines a set of constraint rules for method signatures.

def tag(tag_name): 
    def add_tag(content): 
        return "<{0}>{1}</{0}>".format(tag_name, content) 
    return add_tag 
 
content = &#39;Hello&#39; 
 
add_tag = tag(&#39;a&#39;) 
print add_tag(content) 
# <a>Hello</a> 
 
add_tag = tag(&#39;b&#39;) 
print add_tag(content) 
# <b>Hello</b>

In this example, we want a function to add tags to content, but the specific tag_name will be determined based on actual needs. The interface for external calls has been determined, which is add_tag(content). If implemented in an interface-oriented manner, we will first write add_tag ​​as an interface, specify its parameters and return type, and then implement add_tag ​​of a and b respectively.

But in the concept of closure, add_tag ​​is a function, which requires two parameters: tag_name and content, but the parameter tag_name is packed away. So you can tell me how to pack it at the beginning and take it away.

The above example is not too vivid. In fact, the concept of closure is also very common in our life and work. For example, when dialing on a mobile phone, you only care about who the call is to, and you don't worry about how each brand of mobile phone implements it and what modules are used. Another example is when you go to a restaurant to eat, you only need to pay to enjoy the service. You don't know how much gutter oil was used in the meal. These can be regarded as closures, which return some functions or services (making phone calls, dining), but these functions use external variables (antennas, drain oil, etc.).

You can also think of a class instance as a closure. When you construct this class, you use different parameters. These parameters are the packages in the closure. The external methods provided by this class are the functions of the closure. But a class is much larger than a closure, because a closure is just a function that can be executed, but a class instance may provide many methods.

When to use closures

In fact, closures are very common in Python, but you just didn’t pay special attention to the fact that this is a closure. For example, the decorator in Python, if you need to write a decorator with parameters, a closure will generally be generated.

Why? Because Python’s decorator is a fixed function interface form. It requires that your decorator function (or decorator class) must accept a function and return a function:

# how to define 
def wrapper(func1):  # 接受一个callable对象 
    return func2  # 返回一个对象,一般为函数 
     
# how to use 
def target_func(args): # 目标函数 
    pass 
 
# 调用方式一,直接包裹 
result = wrapper(target_func)(args) 
 
# 调用方式二,使用@语法,等同于方式一 
@wrapper 
def target_func(args): 
    pass 
 
result = target_func()

So what if your decorator takes parameters? Then you need to wrap another layer on the original decorator , used to receive these parameters. After these parameters (private goods) are passed to the inner decorator, the closure is formed. So when your decorator requires custom parameters, a closure will generally be formed. (Exception for class decorators)

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(&#39;b&#39;) 
def hello(name=&#39;Toby&#39;): 
    return &#39;Hello {}!&#39;.format(name) 
 
# 不用@的写法如下 
# hello = html_tag(&#39;b&#39;)(hello) 
# html_tag(&#39;b&#39;) 是一个闭包,它接受一个函数,并返回一个函数 
 
print hello()  # <b>Hello Toby!</b> 
print hello(&#39;world&#39;)  # <b>Hello world!</b>

For a more in-depth analysis of decorators, you can read another blog I wrote.

Go a little deeper

In fact, you don’t have to go too deep. If you understand the concepts above, many codes that seem to be a headache are just that.

Let’s take a look at what the closure package looks like. In fact, the closure function has an additional __closure__ attribute compared to the ordinary function, which defines a tuple to store all cell objects. Each cell object stores all the external variables in the closure one by one.

>>> def make_printer(msg1, msg2): 
    def printer(): 
        print msg1, msg2 
    return printer 
>>> printer = make_printer(&#39;Foo&#39;, &#39;Bar&#39;)  # 形成闭包 
 
>>> printer.__closure__   # 返回cell元组 
(<cell at 0x03A10930: str object at 0x039DA218>, <cell at 0x03A10910: str object at 0x039DA488>) 
 
>>> printer.__closure__[0].cell_contents  # 第一个外部变量 
&#39;Foo&#39; 
>>> printer.__closure__[1].cell_contents  # 第二个外部变量 
&#39;Bar&#39;

The principle is that simple.

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How Do I Use Beautiful Soup to Parse HTML?How Do I Use Beautiful Soup to Parse HTML?Mar 10, 2025 pm 06:54 PM

This article explains how to use Beautiful Soup, a Python library, to parse HTML. It details common methods like find(), find_all(), select(), and get_text() for data extraction, handling of diverse HTML structures and errors, and alternatives (Sel

Mathematical Modules in Python: StatisticsMathematical Modules in Python: StatisticsMar 09, 2025 am 11:40 AM

Python's statistics module provides powerful data statistical analysis capabilities to help us quickly understand the overall characteristics of data, such as biostatistics and business analysis. Instead of looking at data points one by one, just look at statistics such as mean or variance to discover trends and features in the original data that may be ignored, and compare large datasets more easily and effectively. This tutorial will explain how to calculate the mean and measure the degree of dispersion of the dataset. Unless otherwise stated, all functions in this module support the calculation of the mean() function instead of simply summing the average. Floating point numbers can also be used. import random import statistics from fracti

Serialization and Deserialization of Python Objects: Part 1Serialization and Deserialization of Python Objects: Part 1Mar 08, 2025 am 09:39 AM

Serialization and deserialization of Python objects are key aspects of any non-trivial program. If you save something to a Python file, you do object serialization and deserialization if you read the configuration file, or if you respond to an HTTP request. In a sense, serialization and deserialization are the most boring things in the world. Who cares about all these formats and protocols? You want to persist or stream some Python objects and retrieve them in full at a later time. This is a great way to see the world on a conceptual level. However, on a practical level, the serialization scheme, format or protocol you choose may determine the speed, security, freedom of maintenance status, and other aspects of the program

How to Perform Deep Learning with TensorFlow or PyTorch?How to Perform Deep Learning with TensorFlow or PyTorch?Mar 10, 2025 pm 06:52 PM

This article compares TensorFlow and PyTorch for deep learning. It details the steps involved: data preparation, model building, training, evaluation, and deployment. Key differences between the frameworks, particularly regarding computational grap

What are some popular Python libraries and their uses?What are some popular Python libraries and their uses?Mar 21, 2025 pm 06:46 PM

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

Scraping Webpages in Python With Beautiful Soup: Search and DOM ModificationScraping Webpages in Python With Beautiful Soup: Search and DOM ModificationMar 08, 2025 am 10:36 AM

This tutorial builds upon the previous introduction to Beautiful Soup, focusing on DOM manipulation beyond simple tree navigation. We'll explore efficient search methods and techniques for modifying HTML structure. One common DOM search method is ex

How to Create Command-Line Interfaces (CLIs) with Python?How to Create Command-Line Interfaces (CLIs) with Python?Mar 10, 2025 pm 06:48 PM

This article guides Python developers on building command-line interfaces (CLIs). It details using libraries like typer, click, and argparse, emphasizing input/output handling, and promoting user-friendly design patterns for improved CLI usability.

Explain the purpose of virtual environments in Python.Explain the purpose of virtual environments in Python.Mar 19, 2025 pm 02:27 PM

The article discusses the role of virtual environments in Python, focusing on managing project dependencies and avoiding conflicts. It details their creation, activation, and benefits in improving project management and reducing dependency issues.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment