Python Learning (ix)--[advanced] Function

Source: Internet
Author: User

    • Closed Package
Python functions can be nested, you can put one function in the other.
def multiplier (factor):     def multiplybyfactor (number):         return number*factor    return multiplybyfactor
        Call multiplier (), the inner function is returned, which means the function itself is returned, but it is not called. It is important that the returned function also has access to the scope where its definition is located.         An inner function is defined in an outer function, the inner function uses the temporary variable of the outer function, and the return value of the outer function is a reference to the inner function, thus constituting a closure. If the outer function finds its temporary variable to be used in the inner function at the end, it binds the temporary variable to the inner function and then ends itself.         Using closures, once an outer function has been called to return a reference to an inner function, although each call to an inner function is to open a function after the execution of the extinction, but the closure variable is actually only one copy, Each open inner function uses the same closed-packet variable.         can look at an example:
def count ():     = []    for in range (1, 4):        def  F ():              return i*i        fs.append (f)    return= count ()
In this example, each loop creates a new function and then puts the function name into the list FS without calling function F, which means that the internal calculation is not done. The last three functions are returned. Note here that the outer function variable fs,i is only one, so after 3 cycles, I becomes 3. So finally we find that the result of calling F1 (), F2 (), F3 () returns is 9.
>>> F1 ()9>>> F2 () 9>>> f3 ()9
    • anonymous functions
Lambda, as an expression, defines an anonymous function. For example: Lambda x:x+1. A function is actually defined, just like the effect below.
    def Fun (x)         return (x+1)
    • Decorative Device
In Python, the function is also an object, and the function object has a __name__ property that can get the name of the function:
def name (): ...      Print ' Tom ' ... >>> name. __name__ ' name '
if you want to enhance the functionality of the name () function, but do not want to modify the definition of the name () function, this way of dynamically adding functionality during the run of the code is called an adorner. an adorner is a higher-order function that returns a function, such as an adorner that defines a print log:
def log (func):     def Wrapper (*args, * *kw)        :print'call%s ():' % func.  __name__        return func (*args, * *kw    )return Wrapper
Log is an adorner that takes a function as an argument and returns a function. So when we use adorners, we need to use the @ syntax:
@log def name ():     Print ' Tom'
More than now calls name (), which is equivalent to executing the name = log (name) and then executing name (). 
    • Partial function
The Python functools module provides the functionality of the slice function. for example, the Int () function can convert a string to an integer, or it can pass in the base parameter, setting the conversion's binary. such as int (' 1234 ', base = 8). If you want to convert a large number of octal strings, each time you pass in a binary parameter, you can:
def int8 (x,base = 8)    return int (x,base)
We can also use the slice function to achieve the same effect, namely:
int8 = functools.partial (int, base=2)

  Reference article: Https://www.liaoxuefeng.com/wiki/001374738125095c955c1e6d8bb493182103fac9270762a000/ 001386819879946007bbf6ad052463ab18034f0254bf355000

Python Learning (ix)--[advanced] Function

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.