Python High-order functions

Source: Internet
Author: User

1. The function itself can also be assigned to a variable, that is: A variable can point to a function. The function name is actually a variable pointing to the function!

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M02/9B/EB/wKiom1loo-3htg2zAABIPnuFOsg743.png "title=" h0c% QSG1~G%RTXU2 (1$9i%j.png "alt=" Wkiom1loo-3htg2zaabipnufosg743.png "/>

2. A variable can point to a function , The function's parameters can receive the variable , then a function can receive another function as a parameter , This function is called the higher order function .

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/9B/EB/wKiom1lopC2TVKDVAAAqf3lNqM4782.png "title=" Rrrjqpb@ulql5ctxq ' R@PKD.png "alt=" Wkiom1lopc2tvkdvaaaqf3lnqm4782.png "/>

the 3.map () function receives two parameters , One is a function , the other is a sequence , and a map Functions the passed-in function sequentially to each element of the sequence and returns the result as a new list

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/9B/EB/wKioL1lopGiCbHiOAAAgj6s_QaM636.png "title=" 2$3p{ clcg2s23me@u% () h94.png "alt=" Wkiol1lopgicbhioaaagj6s_qam636.png "/>

4. Thereduce function uses a function to sequence in a column [X1, x2, x3 ...] , the function must receive two parameters, and reduce accumulates the result and the next element of the sequence.

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M01/9B/EB/wKioL1lopNGxVYDPAAAcunpVJIU373.png "title=" 38bzm7p0sck9f}t}q6w~ufu.png "alt=" Wkiol1lopngxvydpaaacunpvjiu373.png "/>

 5. filter () also receives a function and a sequence. and map () different time ,filter () the incoming function is applied sequentially to each element true or false decide whether to keep or discard the element.

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/9B/EC/wKiom1lopUHQXz3HAAAfsRQ1wu0954.png "title=" 3IV65PQ_GQJQMJK ' Purz59a.png "alt=" Wkiom1lopuhqxz3haaafsrq1wu0954.png "/> 6. python built -in Sorted () function can be used to List To sort from small to large

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M01/9B/EC/wKiom1lopZ7THTMlAAAWON2TPdg179.png "title=" 1~ Nkpv%u]%$}0w[a%nzcs8n.png "alt=" Wkiom1lopz7thtmlaaawon2tpdg179.png "/>

Sorted reverse Order

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/9B/EB/wKioL1loph7zpmw9AAAsYGDwPUE995.png "title=" nl6{(%kz@h%ex%%9}i7ss{r.png "alt=" Wkiol1loph7zpmw9aaasygdwpue995.png "/> 7. * Anonymous function. when we pass in the function,There are times,you do not need to explicitly define a function,It is more convenient to pass in anonymous functions directly. Key WordsLambdarepresents an anonymous function,before the colonxrepresents a function parameter.

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/9B/EE/wKiom1loymSguePEAAAUEo-9hEw117.png "title=" x~ 14u9d@3_t) eco[rkvw3ir.png "alt=" Wkiom1loymsguepeaaaueo-9hew117.png "/>

* Anonymous functions have only one expression , don't write . return, The return value is the result of the expression.

* Because anonymous functions have no name , There is no need to worry about function name collisions. the anonymous function is also a function object , You can assign an anonymous function to a variable , and then use the variable to invoke the function.

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/9B/EE/wKiom1loyzyQZ2-qAAATcVt-Jhs283.png "title=" V) PO %) 65zp%3q (msvj1m0w5.png "alt=" Wkiom1loyzyqz2-qaaatcvt-jhs283.png "/>

* The anonymous function can also be returned as a return value

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/9B/ED/wKioL1loy4_THc3-AAAvSU8Smb4629.png "title=" B% Afvjv1hf3bxc}5r_b5{dr.png "alt=" Wkiol1loy4_thc3-aaavsu8smb4629.png "/>

8. adorners are used to decorate functions.

Want to enhance the function of the original function;

But you do not want to modify the definition of the now () function;

How you dynamically add functionality while your code is running

650) this.width=650; "src=" Https://s2.51cto.com/wyfs02/M01/9B/ED/wKioL1lozB-T4KGPAABrz1fMWyU433.png "title=" UV2] Ms7yp]944xnf~}%t]6o.png "alt=" Wkiol1lozb-t4kgpaabrz1fmwyu433.png "/>

Python High-order functions

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.