Python functools.wraps Decorator Module

Source: Internet
Author: User

#-*-coding=utf-8-*-__author__='Piay'ImportTime , Functoolsdeffoo ():" "define a normal function: return:" "    Print 'This is foo'foo ()" "here, if we need to look at the function execution time, modify it to:" "deffoo1 (): Start_time=Time.clock ()Print 'This is foo1'End_time=Time.clock ()Print 'The execution time is:', End_time-start_timefoo1 ()" "if our other functions also require execution time, or if the function does not require execution time, then we need to copy it to the other functions. That's the worst way." "defFoo3 ():Print 'This is Foo3'defTimeit (func):" "we could consider redefining a function Timeit, passing the reference to Foo, and then calling Foo in Timeit and timing it so that we can achieve the purpose of not altering the Foo definition:p aram Func: The function passed in: return: " "start_time=Time.clock () func () End_time=Time.clock ()Print 'used:', End_time-Start_timetimeit (Foo3)" "This writes the code that modifies the calling section. That's what we call it: Foo3 (), now Timeit (foo), so if Foo is called at N, you have to change the code in the N place. Or, to be more extreme, consider that the code in which it is called cannot modify the situation, for example: This function is something you give to someone else. " "" "think of a way to not modify the code of the call, if you do not modify the calling code, it means that calling Foo () requires the effect of calling Timeit (foo). We can think of assigning Timeit to Foo, but Timeit seems to have a parameter ... Think of ways to unify the parameters! If Timeit (foo) does not directly produce a call effect, but instead returns a function that is consistent with the Foo argument list ... It's good to have the return value of Timeit (foo) assigned to Foo, and then the code that calls Foo () doesn't have to be modified at all! " "defFoo4 ():Print 'This is Foo4'#Define a timer, pass in one, and return another method with the timer feature attacheddefTIMEIT4 (func):#define an inline wrapper function that adds a timing function to the incoming function    defwrapper (): Start_time=Time.clock () func () End_time=Time.clock ()Print 'used:', End_time-start_time#returns the Wrapped function    returnWrapperfoo_1=Timeit (Foo4)" "The above code is like an adorner, which can be modified as follows:" "@timeit4#adding this line to the definition is exactly equivalent to the other write foo = Timeit (foo)deffoo5 ():Print 'This is foo5'Foo5 ()" "-----------------------------------------------functions using the Functools.wraps (func) Adorner" "defTimeit_3_for_wraps (func): @functools. Wraps (func)defwrapper (): Start=Time.clock () func () End=Time.clock ()Print 'used:', end-StartreturnWrapper@timeit_3_for_wrapsdefFoo6 ():Print 'This is Foo6'Foo6 ()

Python functools.wraps Decorator Module

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.