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.Statistics of function execution time
2.output log information
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 )
func1()
Results of the:
...
99995
99996
99997
99998
99999
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.
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 )
func1()
Results of the:
...
99995
99996
99997
99998
99999
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
-
A detailed explanation of Hadoop core architecture HDFS
Knowledge Base Team
-
What Does IOT Mean
Knowledge Base Team
-
6 Optional Technologies for Data Storage
Knowledge Base Team
-
What Is Blockchain Technology
Knowledge Base Team
Explore More Special Offers
-
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