Briefly talk about closures in Python
Closures in Python
Someone left a message a few days ago, and it is unclear about the use of one of the closures
and re.sub
. I searched on Script Home and found that I had never written anything related to closures, so I decided to summarize and improve the content of Python.
1. The concept of closure
First of all, we have to start with the basic concept. What is closure? Let’s take a look at the explanation on Wiki:
....
Two key points are mentioned above: free variables and functions. These two keys will be discussed later. I still have to elaborate on the meaning of "closure". As the text makes clear, it can be understood vividly as a closed package. This package is a function. Of course, there is also the corresponding logic inside the function. What is inside the package is freedom. Variables, free variables can wander around with the package. Of course, there must be a premise that this package is created.
For example:
def func(name): def inner_func(age): print 'name:', name, 'age:', age return inner_func bb = func('the5fire') bb(26) # >>> name: the5fire age: 26
When func is called here, a closure - inner_func is generated, and the closure holds the free variable - name, so this also means that when the life cycle of function func ends, the variable name still exists. Because it is referenced by the closure, it will not be recycled.
In addition, closure is not a unique concept in Python. All languages that treat functions as first-class citizens have the concept of closure. However, closures can also be used in languages like Java where classes are first-class citizens, but they must be implemented using classes or interfaces.
For more conceptual things, please refer to the reference link at the end.
2. Why use closures
Based on the above introduction, I wonder if readers feel that this thing is somewhat similar to classes. The similarity is that they both provide data encapsulation. The difference is that the closure itself is a method. Just like classes, we often abstract common things into classes when programming (of course, as well as modeling the real world-business) to reuse common functions. The same is true for closures. When we need function-granular abstraction, closures are a good choice.
At this point, a closure can be understood as a read-only object. You can pass a property to it, but it can only provide you with an execution interface. Therefore, in programs we often need such a function object - closure, to help us complete a common function, such as the decorator which will be mentioned later.
3. Use closures
The first scenario, a very important and common usage scenario in Python is the decorator. Python provides a very friendly "syntax sugar" for the decorator - @, which allows us to use the decorator very conveniently. I won’t elaborate too much on the principle of decoration. In short, if you add @decorator_func to a function func, it is equivalent to decorator_func(func):
def decorator_func(func): def wrapper(*args, **kwargs): return func(*args, **kwargs) return wrapper @decorator_func def func(name): print 'my name is', name # 等价于 decorator_func(func)
In this example of the decorator, the closure (wrapper) holds the external func parameter and can accept external parameters. The accepted parameters are passed to func intact and the execution result is returned.
This is a simple example. If it is slightly more complicated, you can have multiple closures, such as the frequently used LRUCache decorator. The decorator can accept parameters like @lru_cache(expire=500). The implementation is the nesting of two closures:
def lru_cache(expire=5): # 默认5s超时 def func_wrapper(func): def inner(*args, **kwargs): # cache 处理 bala bala bala return func(*args, **kwargs) return inner return func_wrapper @lru_cache(expire=10*60) def get(request, pk) # 省略具体代码 return response()
Students who don’t know much about closures must be able to understand the above code. This is an interview question we often asked in previous interviews.
The second scenario is based on a feature of closure-"lazy evaluation". This application is more common when accessing the database, for example:
# 伪代码示意 class QuerySet(object): def __init__(self, sql): self.sql = sql self.db = Mysql.connect().corsor() # 伪代码 def __call__(self): return db.execute(self.sql) def query(sql): return QuerySet(sql) result = query("select name from user_app") if time > now: print result # 这时才执行数据库访问
The above inappropriate example shows the function of lazy evaluation through closure, but the result returned by the above query is not a function, but a class with functional functions. If you are interested, you can take a look at the implementation of Django's queryset. The principle is similar.
The third scenario is the situation where the parameters of a certain function need to be assigned values in advance. Of course, there is already a good solution in Python to access functools.parial, but it can also be achieved using closures.
def partial(**outer_kwargs): def wrapper(func): def inner(*args, **kwargs): for k, v in outer_kwargs.items(): kwargs[k] = v return func(*args, **kwargs) return inner return wrapper @partial(age=15) def say(name=None, age=None): print name, age say(name="the5fire") # 当然用functools比这个简单多了 # 只需要: functools.partial(say, age=15)(name='the5fire')
It seems like this is another far-fetched example, but it can be regarded as a practical application of closures.
Finally, to summarize, closures are easy to understand and are widely used in Python. This article is a summary of closures. If you have any questions, please leave a message.
4. References
Wikipedia - Closures
http://stackoverflow.com/questions/4020419/closures-in-python
http://www.shutupandship.com/2012/01/python-closures-explained.html
http://stackoverflow.com/questions/141642/what-limitations-have-closures-in-python-compared-to-language-x-closures
http://mrevelle.blogspot.com/2006/10/closure-on-closures.html

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Python is widely used in the fields of web development, data science, machine learning, automation and scripting. 1) In web development, Django and Flask frameworks simplify the development process. 2) In the fields of data science and machine learning, NumPy, Pandas, Scikit-learn and TensorFlow libraries provide strong support. 3) In terms of automation and scripting, Python is suitable for tasks such as automated testing and system management.

You can learn basic programming concepts and skills of Python within 2 hours. 1. Learn variables and data types, 2. Master control flow (conditional statements and loops), 3. Understand the definition and use of functions, 4. Quickly get started with Python programming through simple examples and code snippets.

It is impossible to view MongoDB password directly through Navicat because it is stored as hash values. How to retrieve lost passwords: 1. Reset passwords; 2. Check configuration files (may contain hash values); 3. Check codes (may hardcode passwords).

As a data professional, you need to process large amounts of data from various sources. This can pose challenges to data management and analysis. Fortunately, two AWS services can help: AWS Glue and Amazon Athena.

The steps to start a Redis server include: Install Redis according to the operating system. Start the Redis service via redis-server (Linux/macOS) or redis-server.exe (Windows). Use the redis-cli ping (Linux/macOS) or redis-cli.exe ping (Windows) command to check the service status. Use a Redis client, such as redis-cli, Python, or Node.js, to access the server.

To read a queue from Redis, you need to get the queue name, read the elements using the LPOP command, and process the empty queue. The specific steps are as follows: Get the queue name: name it with the prefix of "queue:" such as "queue:my-queue". Use the LPOP command: Eject the element from the head of the queue and return its value, such as LPOP queue:my-queue. Processing empty queues: If the queue is empty, LPOP returns nil, and you can check whether the queue exists before reading the element.

Question: How to view the Redis server version? Use the command line tool redis-cli --version to view the version of the connected server. Use the INFO server command to view the server's internal version and need to parse and return information. In a cluster environment, check the version consistency of each node and can be automatically checked using scripts. Use scripts to automate viewing versions, such as connecting with Python scripts and printing version information.

Navicat's password security relies on the combination of symmetric encryption, password strength and security measures. Specific measures include: using SSL connections (provided that the database server supports and correctly configures the certificate), regularly updating Navicat, using more secure methods (such as SSH tunnels), restricting access rights, and most importantly, never record passwords.
