Python-functional programming (passing functions as parameters)

Source: Internet
Author: User

Map: Accepts two parameters (function, iterable), map passes the incoming function to each element of iterable, and returns the new iterable
def f (x):     return x*= map (f,[1,2,3,4])    # at this time R is lazy evaluation-can be used next () and for...in value # return all through list () Print (List (R))  # [1, 4, 9, +]
Reduce: Accepts two parameters (function, sequence), reduce takes a function on a sequence, and the returned result continues to accumulate with the next element of the sequence, with the effect of: reduce (F,[x,y,z]) =>f (f (x, y), z)
 from Import Reduce def Add (x, y    ): return x+yreduce (add,[#
Filter: Filter sequence. Accept two parameters (function, sequence), filter to function on the sequence, depending on whether the return value is true, decide whether to discard the element
def is_odd (x):     return x%2==1list (filter (is_odd,[1,2,3,4,5,6,7,8,9))    #Filter Lazy evaluation [1, 3, 5, 7, 9]
Sorted: Sort, sortable objects include number list, string list, dict, etc., can accept three parameters, the latter two are optional
# sorted ([],key=express,reverse=true) key: How to handle each element   reverse: Reverse sort #[8, 6, 3,-2, 1,-1, 0] 
Anonymous FunctionsLambda: anonymous function keyword: The preceding element represents the parameter of an anonymous function anonymous function without thanking the return expression, the return value is the value of the expression
m = List (map (Lambda x:x*x,[1,2,3,4))print(m)    #[1, 4, 9, +]
Decorative Device: Dynamically increasing functionality during code run
#automatically print logs before the now function runsImportFunctoolsdeflog (func): @functools. Wraps (func)defWrpper (*args,**kw):Print("Call %s ()"% func.__name__)        returnFunc (*args,**kw)returnWrpper#Call Adorner@logdefNow ():Print("Hello") Now ()#Call Now () Hello
Partial function: functools.partial Fixed some parameters of the function, returning a new function to make the call simpler
Import= functools.partial (int,base=2)print(Int2 ("1000000" ))  #

Python-functional programming (passing functions as parameters)

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.