Python Adorner, closure function

Source: Internet
Author: User
Tags wrapper

    1. Closure function: Defines another function B within function A, followed by B as the return value of a is returned directly. At this point, function B is called a closure function. In the closure function B, if the variable defined in the A function is used, the variable defined in the A function is temporarily stored until the variable is retracted by the system at the end of the B function call, enabling the variable delay release in a
For example
Put (num):
Print (num)
Wrapper ():
Num
Num +=20
Print (num)
Wrapper
Fun = put (0)
Fun ()

This function runs as follows: Runs from top to bottom until fun = put (0) statement, which is called function put, executes the function put, the output is 0,fun = wrapper, returns a function to fun, but not called. When you continue to execute program fun (), the program runs the wrapper function, since Num already has a value (nonlocal is not the local variable), so the function outputs 20

    1. Global: The declared variable is a global variable, and when the variable is used, the computer looks directly to the outermost part of the function for the variable existence
    2. nonlocal: Declaring a variable to be a non-local variable, at which time the computer does not look for the variable in the function used by the current variable, but rather to the outer layer of the function looking for a corresponding variable close to the function to declare
    3. Adorner: The function of the original function is augmented by the use of the @ syntax to modify functions that already existed before using the defined function directly

Advantages: No need to modify the original code, you can achieve the function of the original function of the expansion, facilitate the maintenance of late code

* * (the adorner is essentially a Python function that allows other functions to add additional functionality without any code changes, and the adorner is also a function object.) He is often used for scenarios where there is a need for facets, such as inserting logs, performance testing, transaction processing, and so on. Decorators are a great design for solving such problems, and with adorners, we can pull out a lot of similar code that is independent of the function itself and continue to reuse it.

ImportTime
ImportFunctools
defLog_time (Fun):
@functools. Wraps (Fun)
defJJ (*ARGS1,**KB):
Print (Time.strftime ("%y-%m-%d%h:%m:%s", Time.localtime ()))
returnFun (*ARGS1,**KB)
returnJj
@log_time#Now = Log_time (now)
defNow (*a):
Print (a)
Now (19,20)
#define an adorner with its own parameters
defLog (TXT):
defDe (fun):
@functools. Wraps (Fun)
defWrapper (*M,**KM):
Print (Txt,end ="')
Print (Time.strftime ("%y-%m-%d%h:%m:%s", Time.localtime ()))
returnFun (*m,**km)
returnWrapper
returnDe
@log ('the corresponding time is: ')#equivalent to Max_num = log (' Corresponding time is: ') Fun (max_num)
defMax_num (NUM1,NUM2):
Print (max (num1,num2))
Max_num (2,43)
    1. Get absolute Value: Print (ABS (-4))

List traversal:

def callBack (num):
Num **= 2
Return num
List1 = [x for x in range (10)]
result = Map (callback,list1)
Print (Result) #map (fun,list): Map is used to iterate through the list of lists, and each traversal automatically calls the fun function to complete the calculation of the elements resulting from this traversal, returning a map object when the traversal is over

6. List filter:

List2 = [1,3,4,54,5,4,3,35, "SDF", "DSF"]
Li = Filter (lambda x:isinstance (x,int), List2)
Print (LI) #列表过滤: Filter (fun,list) is used to iterate through the list of lists to complete the operation of saving the data that satisfies the condition and filtering (deleting) The condition according to the given filter condition. Where fun is a callback function that acts as a filter condition

7. #偏函数

Import Functools
Int2 = functools.partial (int,base = 2) #将指定的函数和函数中默认值的参数进行绑定, generate a new function, such as the default argument base binding of the INT function and int to generate a new function Int2, Int2 converting by binary at this time for data conversion

result = Int2 ("10101000010100")
Print (Result)

Python Adorner, closure function

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.