Python Advanced - Use of Decorators

Python Advanced - Use of Decorators Introduction:

Python Advanced - Use of Decorators. Statistics of function execution time and output log information

1. Python Advanced - Use of Decorators.Use scenarios of decorators

1.Statistics of function execution time
2.output log information

2. Python Advanced - Use of Decorators.The decorator implements the statistics of the execution time of existing functions

import time
# decorator function
def get_time ( func ):
def inner( ):
begin = time.time ()
func ( )
end = time.time ()
print("Function execution cost %f" % (end-begin))
return inner
@get_time _
def func1():
for i in range( 100000):
print( i )
Results of the:
Function execution cost 0.329066

2. Summary
decorator can be known from the above sample code :
•Under the premise of not changing the source code and calling method of the existing function, the function of the existing function is extended.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us