Wudao road-five-day peak during the refining stage

Source: Internet
Author: User

Wudao road-five-day peak during the refining stage

A function is a variable.

Function nesting: Define a function call in a function. (multiple layers can be nested. Note that local scopes and global scopes are distinguished)

Def test ():

Def testx ():

Test ()

High-order functions: pass a function name as a real parameter to another function or the function return value contains the function name.

Def f_test (func ):

Func ()

Def f_test1 ():

Print ('aaa ')

F_test (f_test1) # z calls f_test here, And f_test1 will be called in function f_test (without changing its source code and outputting aaaa)

Def f_test2 (func ):

Print ('some functions ')

Return func

F_test1_address = f_test2 (f_test1) # print f_test1_address. We can see that when f_test2 is called, The passed real parameters will be returned.

F_test1_address () # equals to execute f_test2, f_test1 (do not change its call method and add some output functions)

Decorator (Higher-order functions + nested Functions): Do not change the function call method and source code;

Def f1 (func): # decorator

Def f2 ():

Func ()

    Print ('function ')

Return f2

@ F1 # This is equivalentF3 = f1 (f3 ))That is, add the name of the @ modifier before the function definition to Add decoration to the function.

Def f3 ():

Print ('f3 ')

  

F2_address = f1 (f3) =>Define An f2 in the f1 function and return f2 (and f2 runs f3 (the passed function name and the passed real parameter is f3 ), and outputs functions)

At this time, f2_address () is equivalent to running f2, that is, running and outputting the function. If you replace f2_address with f3 (F3 = f1 (f3 ))

Then calling f3 will execute one more print ('function') (here you can write your additional features)

 

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.