Calculating a function of time with the decorator

! # / User / bin / Python 
# - * - Coding: UTF-. 8 - * -
#. 1,
# L = [l, 3]
# A = L .__ ITER __ () is equal to # ITER (L)
# Print (A .__ the Next __ ())
# Print (a .__ the Next __ ())
# 2, decorator essentially function, function is to add other functions additional functions, principle: do not modify the modified function source code, without modifying the calls are decorated function way
# Import Time
# DEF Timmer (FUNC):
# DEF wapper (* args, ** kwargs):
# start_time = time.time ()
# RES = FUNC (* args, ** kwargs)
# stop_time = time.time ( )
# Print ( "a function of time was S%"% (stop_time - START_TIME))
# return RES
# return wapper
# @timmer
# CAL DEF (L):
# RES = 0
# for I in L:
# the time.sleep ( 0.1)
# I = RES +
RES return #
# RES = CAL (the Range (20))
# Print (RES)
# Print ( "the end of .......................... .................. ")
# 3, higher order functions decorator = + + nested function closure
# 4, higher-order function: the function is a function of the received parameters name, the return value of the function is a function name,
# DEF foo ():
# Print ( "Hello Zhang master")
# DEF the Test (func):
# Print (func) # this is the print func memory address
# start_time the time.time = ()
# the time.sleep (2)
# FUNC ()
# = stop_time the time.time ()
# Print ( "run time was S%"% (stop_time - START_TIME))
# Test (foo)
#. 5,
DEF foo # ():
# Print ( "from at The foo")
# DEF the Test (FUNC):
# return FUNC
# RES = the Test (foo)
# Print (RES)
# RES ()
# the Test foo = (foo)
Print # (foo)
# foo ()
#. 6,
# Import Time
# # DEF foo ():
# # the time.sleep (. 3)
# # Print ( "from foo")
# # # foo without modifying the source code, without modifying foo is called by
# # DEF Timmer (FUNC):
# = # START_TIME the time.time ()
# FUNC # ()
# = # stop_time the time.time ()
# # Print ( "a function of time was% s"% (stop_time - START_TIME))
# # return FUNC
# # foo = Timmer (foo)
# # foo ()
#. 7,
# DEF Father (name):
# Print ( "from Father% S"% name)
# DEF Son ():
# Print ( "from Son")
# DEF Grandson ():
# Print ( "from Grandson")
# Grandson ()
# Son ()
Father # ( "ZD")
# Print ( "top end of run ............")
# DEF Father (AUTH_TYPE):
# # Print ( "from Father% S"% name)
# DEF Son ():
# name = "ZS"
# Print ( "my father is S%"% AUTH_TYPE)
# # Print (about locals ()) #locals Print this layer is locally variable
# Son ()
# father ( "ZD ")
# 8, the decorative frame
Import time
DEF Timmer (FUNC):
DEF warpper (* args, ** kwargs): # * args parameter representative of the position of all received into tuple representing the user pass the time ** kwargs value may be the key
# Print (FUNC)
start_time = time.time ()
RES = FUNC (* args, ** kwargs) # is running the Test
stop_time = time.time ()
Print ( "time is running% s"% (stop_time - start_time))
return RES
return wrapper # finally return completion status
@timmer # = Timmer is equivalent to the Test (the Test)
DEF the Test (name, Age):
the time.sleep (3)
Print ( "the Test function has finished running, the name [% s] age [% s]"% (name, Age))
return "this is a test of the return value"
@timmer
DEF test1 (name, Age, Gender):
the time.sleep (1)
Print ( "test1 function is finished running, the name [% s] [% s] age gender [% s] "% (name, Age, gender))
return" which is the return value test1 is "
# RES = Timmer (Test) # returns wrapper address
# res () # perform the wrapper ()
# Test = Timmer (Test)
# Test ()
RES = Test ( "linhaifeng", 18 is) # is running warpper
# Print (RES)
test1 ( "Alex", 18 is, "MALE")
#. 9,
# DEF test1 () :
# the time.sleep (3)
# Print ( "the Test function has finished running.")
# Return "Hey, and"
# Res = test1 () # brackets just calls, only after an assignment in parentheses and print return value of the function, the default is None, you can also define a list or return plus a string or other value, return should be written in the current function inside
# print (res)

Guess you like

Origin www.cnblogs.com/zhang-da/p/10972906.html