Python re-discussion decorator

Source: Internet
Author: User

The adorner is essentially a function, a function that decorates other functions. The adorner function takes the function name of the decorated function and returns the function name of the decorated function. There are two principles for decorating a function: one is that the source code of the decorated function cannot be modified, and the other is that the calling method of the decorated function can not be changed.

#the poem () function is decorated with the DEC () function, but the calling method is changed from poem () to name (),#The calling method has changed ... This violates the second principle of the writer's decorator. Workaround: Put name=#The name of Deco (poem) is changed to poem. That is: Name=deco (name), see the next section of code. defDeco (func):#receives a function name that returns the same function name    Print('Appreciation of ancient poems')#Add a decorative statement    returnfuncdefpoem ():Print('Moonset Wu Ti Frost all over the sky, Jiangfeng Isaribi to sorrow sleep. ') name= Deco (poem)#Pass in the function name poem, and then assign the returned name poem to nameName ()#after decorating, call the decorated function again" "Ancient Poetry Appreciation Moonset Wu Ti Frost, Jiangfeng Isaribi to worry sleep. " "
View Code

#the poem () function is decorated with the DEC () function, but the calling method is changed from poem () to name (),#The calling method has changed ... This violates the second principle of the writer's decorator. Workaround: Put name=#The name of Deco (poem) is changed to poem. That is: Name=deco (name), see the next section of code. defDeco (func):#receives a function name that returns the same function name    Print('Appreciation of ancient poems')#Add a decorative statement    returnfuncdefpoem ():Print('Moonset Wu Ti Frost all over the sky, Jiangfeng Isaribi to sorrow sleep. ') Poem= Deco (poem)#Pass in the function name poem, and then assign the returned name poem to namePoem ()#after decorating, call the decorated function again" "Ancient Poetry Appreciation Moonset Wu Ti Frost, Jiangfeng Isaribi to worry sleep. " "
View Code

Write a decorated statement with @:

defDeco (func):#receives a function name that returns the same function name    Print('Appreciation of ancient poems')#Decorative Statements    returnFunc@deco#write a decorative statement on the head of the decorated function, equivalent to poem = Deco (poem)defpoem ():Print('Moonset Wu Ti Frost all over the sky, Jiangfeng Isaribi to sorrow sleep. ')#poem = Deco (poem) #传进去函数名poem and assigns the returned function name poem to namePoem ()#after decorating, call the decorated function again" "Ancient Poetry Appreciation Moonset Wu Ti Frost, Jiangfeng Isaribi to worry sleep. " "
View Code

The decorator is generally written as a nested function:

#decorating a function with no parametersdefGuess_win (func):#Pass in a function name German_team, return a function name Rooftop_status    defrooftop_status (): Result= Func ()#equivalent to Result=german_team ()        Print('The rooftop is full, please line up! ')        returnResult#handles the decorated function german_team () and returns the processed result    returnRooftop_status#After executing this sentence, return the newly defined function name Rooftop_status to German_team:                          #German_team=rooftop_status@guess_win#This sentence is equivalent to German_team=guess_win (Gernam_team)defGerman_team ():Print('Germany to win! ')    return 'win the Clubhouse tender mold! Lose the Sea work! 'x= German_team ()#because the function of return rooftop_status is: German_team=rooftop_statusPrint(x)" "Germany to win! The rooftop is full, please line up! Win the Clubhouse tender mold! Lose the Sea work! " "
View Code

#decorating a function with parametersdefGuess_win (func):#Pass in a function name German_team, return a function name Rooftop_status    defRooftop_status (*args, * *Kwargs): Result= Func (*args, * *Kwargs)Print('The rooftop is full, please line up! ')        returnresultreturnRooftop_status#After executing this sentence, return the newly defined function name Rooftop_status to German_team:                          #German_team=rooftop_status@guess_win#this is equivalent to German_team=guess_win (Gernam_team), calling the Guess_win () functiondefGerman_team (ARG):Print('%s win! '%Arg)return 'Germany won the Clubhouse tender mold! Lose the Sea work! '@guess_win#this is equivalent to Spain_team=guess_win (Spain_team), calling the Guess_win () functiondefSpain_team (ARG):Print('%s win! '%Arg)return 'Spain wins the Clubhouse tender mold! Lose the Sea work! 'x= German_team ('Germany') y= Spain_team ('Spain')Print(x)Print(y)" "Germany to win! The rooftop is full, please line up! Spain to win! The rooftop is full, please line up! Germany won the Clubhouse tender mold! Lose the Sea work! Spain wins the Clubhouse tender mold! Lose the Sea work! " "
View Code

Python re-discussion decorator

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.