Python Learning notes-inline functions, closures, anonymous functions, higher-order function map, higher-order function filter, high-order function reduce

Source: Internet
Author: User

1 #------------------------------inline Function------------------------------2 #an inline function is a function defined inside a function .3 #instance One4 Print("#------------------------------inline function------------------------------")5 defFunoutone ():6x = 57     defFunin ():8x = 39         Print("My funoutone in funin x =", X)Ten     Print("My Funoutone x =", X) One     returnFunin A  - #Calling Methods - Funoutone () () the  - #Example Two - deffunouttwo (): -x = 5 +     defFunin (): -x = 6 +         Print("My funouttwo in funin x =", X) A     Print("My funouttwo x =", X) at     returnFunin () -  - #Calling Methods - Funouttwo () -  - #Example two: How to refer to variables inside a function, using nonlocal in defFunoutthree (): -x = 10 toy = 20 +     defFunin (): - nonlocal x thex = 18 *y = 38 $         Print("My funoutthree in funin x ="X"y =", y)Panax Notoginseng Funin () -     Print("My Funoutthree x ="X"y =", y) the     returnNone +  A Funoutthree () the  + #example four: observing their invocation - deffunoutfour (): $     defFunin (): $         Print("Funin is called after!") -     Print("Funoutfour is called first!") -     returnFunin the  - Funoutfour () ()Wuyi Print("#--------------------------------------------------------------------") the #-------------------------------------------------------------------- - #closure characteristics of------------------------embedded functions-------------------------- Wu Print("#------------------------closure characteristics of inline functions--------------------------") - #when assigning an inline function to a variable, if the variable is not destroyed, the parameters inside the function are not recycled About defClosurefun (): $x = 1 -     defFunin (): - nonlocal x -x + = 1 A         returnx +     returnFunin the  -A =Closurefun () $b =Closurefun () thec =Closurefun () the  the Print("A,", A ()) the Print("A,", A ()) - Print("A,", A ()) in Print("B,", B ()) the Print("B,", B ()) the Print("C,", C ()) About #conclusion, different variables, pointing to the object are irrelevant!  the      the Print("#--------------------------------------------------------------------") the #-------------------------------------------------------------------- + #------------------------anonymous function, higher order function-------------------------- - Print("#------------------------anonymous function, higher order function--------------------------") the #anonymous functions and higher order functions map (map) filter (filter) reduce (inductive)Bayi  the #anonymous functions: Anonymous functions can increase the readability of code, thin code the #Example One: -A =LambdaX, Y:x +y - Print("Anonymous functions:", A (5,8)) the  the #Example Two: theb =Lambda: [x * x forXinchRange (10)] theList1 =B () - Print("Anonymous functions:", List1) the  the #higher-order function map (map): It is the fact that each value in the sequence is passed as a parameter to the function and another sequence is returned the #Example One:94A = Map (LambdaX:X * 2,[x forXinchRange (10)]) the Print("Higher order function map:", List (a)) the  the #Example Two:98b = Map (LambdaX:X * * 3,[x forXinchRange (10,21)]) About Print("Higher order function map:", list (b)) - 101 #Higher order function filter (filter): If one of the values in the sequence is converted to a bool value and the value is false, it will be excluded from the sequence102 #Example One:103A = Filter (LambdaX:x% 2, (1,2,3,4,5,6,7,8))104 Print("Higher order function filter", List (a)) the #Example Two:106b = Filter (LambdaX:x% 2 = = 0, (1,2,3,4,5,6,7,8))107 Print("Higher order function filter", list (b))108 109 #High- order function reduce: The value of a sequence is passed as a parameter to the function, and finally returned as a value! Before you call the reduce function, remember to import the package the  fromFunctoolsImportReduce111 #Example One: theA = reduce (LambdaX, Y:x + y, (x forXinchRange (6)))113 Print("High- order function reduce", a) the  the #Example Two: theb = Reduce (LambdaX, Y:x * y, (1,2,3,4,5))117 Print("High- order function reduce", B)118 119 Print("#--------------------------------------------------------------------") - #--------------------------------------------------------------------

Python Learning notes-inline functions, closures, anonymous functions, higher-order function map, higher-order function filter, high-order function reduce

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.