A brief talk about closures in Python

Source: Internet
Author: User
closures in Python

A few days ago, there was a message about one 闭包 and re.sub the use of it is not very clear. I searched the script house and found that I had not written anything about closures, so I decided to summarize the contents of Python.

1. Concept of closures

First of all, we have to start with the basic concept, what is closures? Look at the explanations on the wiki:

Copy the Code code as follows:

In computer science, closures (Closure) are short for lexical closures (Lexical Closure) and are functions that reference free variables. This quoted free variable will exist with this function, even if it has left the environment in which it was created. So, there is another argument that closures are entities that are composed of functions and their associated reference environment. Closures can have multiple instances at run time, and different reference environments and the same combination of functions can produce different instances.
....


There are two key points mentioned above: free variables and functions, these two keys are discussed later. Still have to repeat under the meaning of "closure", look at the text, you can visualize it as a closed package, this package is a function, of course, the function of the corresponding logic, the contents of the package is a free variable, free variable can be wandering around with the package. There must be a premise, of course, that the package was created.

In the language of Python, a closure is what you call a function A, and the function a returns a function B to you. The returned function, B, is called a closure. The argument you pass when you call function A is a free variable.

As an example:

def func (name): Def inner_func (age): "  print ' name: ', Name, ' Age: ', age return INNER_FUNCBB = func (' the5fire ') bb (26) # & Gt;>> Name:the5fire age:26

When the func is called, a closure--inner_func is generated, and the closure holds the free variable--name, so it also means that after the life cycle of the function Func ends, the name variable still exists because it is referenced by the closure and is not recycled.

In addition, closures are not a unique concept in Python, and all languages that function as first-class citizens have closures. But closures can also be used in languages such as Java, which are class-class citizens, except that they have to be implemented using classes or interfaces.

More conceptual things can refer to the last reference link.

2. Why use closures

Based on the above introduction, do not know that the reader has no sense that this thing and the class is a bit similar, the similarity is that they all provide the encapsulation of data. The difference is that the closure itself is a method. Like classes, we tend to abstract common things into classes when programming, and (of course, the modeling of the real world-business) to reuse common functionality. Closures are also the same, and closures are a good choice when we need the abstraction of function granularity.

At this point the closure can be understood as a read-only object, you can pass a property to him, but it can only provide you with an interface to execute. So in the program we often need such a function object-closure, to help us complete a common function, such as the one that will be mentioned later-the adorner.

3. Using closures

The first scenario, a very important and common use scene in Python is the adorner, Python provides a very friendly "syntactic sugar"--@ for the adorner, so that we can easily use the adorner, the principle of decoration does not do too much elaboration, In short you add @decorator_func to a function func, which is equivalent to Decorator_func (func):

def decorator_func (func): Def wrapper (*args, **kwargs): Return  func (*args, **kwargs) return wrapper@decorator_ Funcdef func (name): print ' My name is ', name# equivalent to Decorator_func (func)

In this example of the adorner, the closure (wrapper) holds the external func parameter, and is able to accept external arguments, and the received arguments are passed to Func intact and return to the execution result.

This is a simple example, a slightly more complex point can have multiple closures, such as the frequently used LRUCache decorator, the adorner can accept parameters @lru_cache (expire=500). The implementation is a nesting of two closures:

def lru_cache (expire=5): # Default 5s timeout def func_wrapper (func):  def inner (*args, **kwargs):   # Cache handling Bala Bala Bala
  return func (*args, **kwargs)  return inner return Func_wrapper@lru_cache (EXPIRE=10*60) def get (Request, PK) # Omit specific code return response ()

The students who do not understand the closures must be able to understand the above code, which is a question we often ask before interviewing.

The second scenario is based on a feature of the closure--"lazy evaluation". This application is more common in the database access, such as:

# Pseudo-code shows class QuerySet (object): Def __init__ (Self, SQL):  self.sql = sql  self.db = Mysql.connect (). Corsor () # Pseudo code de F __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 # does not perform database access at this time

This less-than-appropriate example shows the ability to perform lazy evaluation through closures, but the result returned by query above is not a function, but a class with function functions. It is interesting to look at the implementation of Django's queryset, similar in principle.

The third scenario, where you need to pre-assign a value to a function's parameters, certainly has a good solution to access functools.parial in Python, but can be implemented with closures.

def partial (**outer_kwargs): Def wrapper (func):  def inner (*args, **kwargs):   for K, V in Outer_kwargs.items (): C2/>kwargs[k] = v   return func (*args, **kwargs)  return inner return wrapper@partial (AGE=15) def say (Name=none, Age=none): Print name, Agesay (name= "The5fire") # of course, Functools is a lot simpler than this. # Just need: Functools.partial (say, age=15) (Name= ' The5fire ')

It seems to be a far-fetched example, but it is also a practice of closure applications.

Finally, the closure of this thing is still very easy to understand, in Python application is also very extensive, this article is a summary of the closure, there are any questions welcome message exchange.

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

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.