Python notes (2) function

Source: Internet
Author: User
Tags closure

Everything in Python is an object, and a function is also considered an object.

several built-in functions are commonly used in Map,reduce, Filter,lamda.

Filter

Filter out elements that meet the filter criteria

s = "@431$%$314&6i7 (431) ^&^%2l#%^i6861642k765e&$%65%^$^# $p%^&*%66757y%*^&%th%*&^%&^$ o$&*^n4637 "Print filter (str.isalpha,s)
Map

Receive two parameters, the first is a function, the second is a list, and the result is returned

L = [Random.randint (0,100) for I in range (Ten)]def Sub50 (a):    return a-50print map (sub50,l)
Reduce

Receive two parameters as a map, but he returns a result that iterates over the second parameter list and calculates it continuously.

# 1+2+3+...+99def F_add (A, B):    return a+bprint reduce (F_add,range (1,100))

In Python3, reduce is removed from the global namespace, and in order to use the--from functools import reduce, the concept expressed in the function of reduce is called the Statute fold.

Map reduce and big data in this case are not the same thing.

Lambda

Lambda is an anonymous function that uses a lambda when it needs a function, but does not bother to name it.

Map (Lambda x:x*x, [x for x in range]) # #等价于def PF (x):    return x * XMAP (PF, [x for X in range (10)])

Map,filter,reduce No list parsing is easy to understand, 3 returns a iterator and is no longer a list.

The callback CallBack is also the function as a parameter, running to CallBack () to return to the previous layer

Closed Bag closure

Functions that bind external variables

Closures and adorners are functions as return values

def pow_x (x):    def Echo (value):        return value**x    return echolst = [Pow_x (2), pow_x (3), pow_x (4)]for p in lst:< C5/>print "Closure", p (2)

1. Nesting functions

2. External variables are used for intrinsic functions

3. The external function returns the intrinsic function

If the external variable is list, it can be changed. But it's just a variable that can't be changed.

Inside the function, be aware of the LEGB principle, when there are local variables, the external variables are no longer called.

Adorner decorator

Decoration is on the basis of the original function, plus some other functions, he is the function as a return value, function as an application of parameters.

Multi-layer nesting is the use of closures, modified function as binding quantitative.

Two-tiered nesting structure

The target function can take parameters

Def time_cost (f):    def _f (*arg, **kwarg):        start = Time.clock ()        a=f (*arg,**kwarg)        end = Time.clock ()        print f.__name__, "Run cost time is", End-start        return a    return _f        @time_costdef list_comp (length):    return [(x, Y) for x in range (length) for y in range (length) if x*y > 25]if __name__ = = ' __main__ ':    list_comp (1 000)

  

Three-layer nested structure

Not only the target function can take parameters, but the adorner can also take parameters

Recursive

Recursion is 1) The function itself calls itself 2) has a clear exit. It is commonly used in the Fibonacci sequence, the traversal of the tree graph, etc., or the structure is defined as recursion. Recursion and looping are equivalent.

Too many recursion times can cause stack overflow, and Python does not support tail recursion optimization.

#字符串取反def Reverse_str (s):    If Len (s) <= 1:        return s    else:        return Reverse_str (s[1:]) + s[0]

  

Python notes (2) 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.