Python adorner, inner function, closure simple comprehension

Source: Internet
Author: User
Tags python decorator

The common function of Python intrinsics and closures is that they are passed to the function as arguments, except that the return is different from the invocation.

1. Python intrinsic function

Example Python intrinsics:

def test (*args):    def Add (*args):         #  Displays the parameters of the call external function        return args    return Add (*args)       #  The direct call to return an intrinsic function results in the following: Test (All-in-all)    


The intrinsic function directly references the external function parameter, and the external function test shows the call to return the internal function Add.
Intrinsic functions are useful when you need to perform complex tasks more than once inside a function, thus avoiding stack repetition of loops and code.

2. Python closures

An intrinsic function can be thought of as a closure. A closure is a function that can be dynamically generated by another function, and can alter and store the value of a variable created outside the function.


Python Closures Example:

def Test (a):    def Add ():                                # no  arguments to reference external functions        return "It's a callable%s"% a       #  directly in intrinsic function using external function parameters    Return add                                #  returns the intrinsic function itself, without returning the result of an intrinsic function call running as follows: A1 = Test (1)           # Dynamically generated a function that can record an external variable <function __MAIN__.ADD>A1 (                   # Call this function it's a callable 1test (1) ()              # itself calls the procedure, first dynamically generates a function to call it's a callable 1

3. Python Decorator

An adorner is essentially a function that takes a function as input and returns another function, usually using the following Python techniques in the adorner:

    • *args and **kwargs

    • Closed Package

    • function as a parameter

Python Adorner Example:

def test (func):                           # Passing functions as Parameters    def add (*args,**kwargs):        print "It's a D"        return func (*args,**kwargs)       # After the internal function is related, return the transfer function call return    adddef func (A, B):    return a+b The result of the operation is as follows: Test (func)                # The Func is passed as a function into the test function to decorate the <function __main__.add>   # decorated function, similar to the closure test (func)           # displayed on the decorated function to call it s a D                  # The middle of the decorating process is printed out 3 more simple method call adorner @test                     # The same function can invoke multiple adorners, do not order in sequence def func (A, B): return a    + b regardless of the function func passed in to test (), the adorner returns a new function that contains the extra statement added by test.
In fact, the adorner does not need to execute the code in the function func, just before the end of the function add calls the function Func to get the result of the Func return and the result of the additional code

When the adorner itself needs to pass parameters:

def test (cmd):                            # passing Parameters    def exc (func):                        # The second step is to pass the function as a parameter         def add (*args,**kwargs):             print "It ' s a%s"% CMD             print "Begin"             return func (*args,**kwargs)         return add    return exc The result of the  operation is as follows: Test (' haha ') < function __main__.exc>  test (' haha ') (func)            # Call the adorner to return the result it's a haha begin 3  simpler method call @test (' haha ' )                     # Test function passed parameter def func (A, B):    return a+b

Python Adorner considerations:
After the func is decorated, the function namespace of func has changed

Executive Func.__name__add

How do I point to the original namespace? Python has its own Functools library directly called the row

Import functoolsdef Test (func):    @functools. Wraps (func)    def Add (*args,**kwargs):        print "It ' s a decorator"        return func (*args,**kwargs)    return add    @testdef func (A, B):    return a+b    run again to view the namespace Func.__name_ _func                  # returns itself

Python adorner, inner function, closure simple comprehension

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.