Function Summary in python-detailed description of the decorator closure

Source: Internet
Author: User
The following is a summary of the function closure in python. I think this is quite good. now I will share it with you and give you a reference. Let's take a look at it with Xiaobian. 1. Preface

A function is also an object that can add attributes and use periods to represent attributes.

If the definition of an internal function contains a reference to an object defined in an external function (an external object can be outside an external function), an internal function is called a closure.

2. decorator

The decorator is to wrap the original functions, so that you can do more without modifying the original code.

The decorator syntax is as follows:

@deco2@deco1def func(arg1,arg2...):  pass

This indicates a function with two decorators, which means: func = deco2 (deco1 (func ))

Syntax:

@decodef func():  pass

Indicates func = deco (func)

The syntax of the parameter decorator is as follows:

@deco(deco_args)def func():  pass

It indicates: func = deco (deco_args) (func)

The syntax is as follows:

@deco2(arg1,arg2)@deco1def func():  pass

It indicates foo = deco2 (arg1, arg2) deco1 (func)

The decorator is actually a function and accepts a function object.

Before executing a function, you can run the preparation code or perform some cleanup after executing the code.

When you see the decorator, you may find some code in it, which defines a function and embeds a call to the target function or at least a reference somewhere in the definition.

3. decorative device functions

The function of the decorator is as follows:

Introduce logs; add timing logic to detect performance; add transaction capabilities to functions

The following example shows how to introduce the timing logic to detect performance:

#! /Usr/bin/env pythonfrom time import ctime, sleepdef tsfunc (func): # The decorator function accepts a function object def wrappedFunc (): print '[% s] % s () called' % (ctime (), func. _ name _) return func () # The function object is called here, that is, the modifier is decorated with the original function, then some operations are performed on the basis of the original function: return wrappedFunc @ tsfunc # decorator def foo (): passfoo () sleep (4) for I in range (2 ): sleep (1) foo ()

The execution result is as follows:

[root@python 420]# python deco.py [Tue Apr 19 16:15:01 2016] foo() called[Tue Apr 19 16:15:06 2016] foo() called[Tue Apr 19 16:15:07 2016] foo() called

In the decorator function, a timestamp is added and the target function is called,The return value of the decorator is a function encapsulated.

4. closure

If an internal function references a variable acting externally (but not globally), the internal function is considered as a closure, variables defined in external functions but referenced or used by internal functions are called free variables.

The main functions of closures are as follows:

Install computing, hide the status, and switch between function objects and scopes at will.

Callback is a function, and closure is also a function, but it can carry a little extra scope.

#!/usr/bin/env pythondef counter(start_at=0):  count = [start_at]  def incr():    count[0] += 1    return count[0]  return incrcount= counter(5)print count()print count()

As shown in the preceding example, the incr function in the function references the external variable count and adds one at a time. Therefore, the variable count is called a free variable.

In the above python function summary, the detailed description of the decorator closure is all the content that I have shared with you. I hope to give you a reference and support for my feet.

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.