python-- decorator Elementary

# Process Decorative formed of: simple decoration has the return value of a parameter universal parameter 
# action decorator 
# principles: Open Closed Principle 
# syntactic sugar: @ 
# a fixed pattern decorator 



Import Time
 # Print (Time. time ()) # get the current time 
# the time.sleep (10) # make the program execution to stop for a while in this position when the 


# DEF Timmer (f): # decorator function 
#      DEF Inner (): 
#          Start = time.time () 
#          RET = f () # garnished function 
#          End = time.time () 
#          Print (End - Start) 
#          return RET 
#      return Inner 
# 
# @timmer # syntactic sugar @ decorator function name
# DEF FUNC (): # was decorated function 
#      the time.sleep (0.01) 
#      Print ( 'Hello everyone good boss good colleague') 
#      return 'Happy New Year' 
# # FUNC = Timmer (FUNC) 
# RET = FUNC () #inner () 
# Print (RET) 
# role decorator - do not want to modify the function is called before or after but also want to add functionality to the original function 
# Timmer is a decorator function, but there are some decorative function of a 

# principle : open closed principle 
#    open: extended open 
#    closed: modifications closed 

# seal version 

# DEF Outer (): 
#      DEF Inner (): 
#          return 'Inner' 
#      Inner () 
# 
# Outer () 

#Decorative decorator with parameters function 
# DEF Timmer (F): a function of decorator # 
#      DEF Inner (* args, ** kwargs): 
#          # (1,2) / (. 1) 
#          Start the time.time = () 
#          ret = f (* args, ** kwargs) #f (1,2) # decorated function 
#          End = the time.time () 
#          Print (End - Start) 
#          return RET 
#      return Inner 
# 
# @timmer # syntax sugar @ decorator function name 
# DEF FUNC (a, b): # was decorated function 
#      the time.sleep (0.01) 
#      Print ( 'Hello everyone good boss good colleague', a, b) 
#      return 'Happy New Year' 
# 
#@timmer # syntactic sugar @ decorator function name 
# DEF func1 (A): # was decorated function 
#      the time.sleep (0.01) 
#      Print ( 'Hello everyone good boss good colleague', A) 
#      return 'Happy New Year' 
# Timmer FUNC = # (FUNC) 
# RET = FUNC (1,2) #inner () 
# RET FUNC = (. 1, B = 2) #inner () 
# Print (RET) 

# DEF warpper (F): # decorator function, f is the decorated function 
#      DEF Inner (* args, ** kwargs): 
#          '' 'before being decorative function to do' '' 
#          RET = f (* args, ** kwargs) is # decorative function 
#          '' 'after being decorative function to do' ''
#         return ret
#     return inner
#
# @Wrapper # syntactic sugar @ decorator function name 
# DEF FUNC (A, b): # was decorated function 
#      the time.sleep (0.01) 
#      Print ( 'Hello everyone good boss good colleague', A, b) 
#      return 'Happy New Year' 

# DEF wrapper (): 
#      DEF Inner (): 
#          Pass 
#      return Inner 

DEF wrapper (FUNC):    # qqxing 
    DEF Inner (* args, ** kwargs): 
        RET = FUNC (* args, ** kwargs )    # decorated function 
        return RET
     return Inner 

@wrapper         # qqxing = warpper (qqxing) 
DEFqqxing ():
     Print (123 ) 

RET = qqxing ()    # Inner 

# 
# explain the extended New Year's job decorator

 

Guess you like

Origin www.cnblogs.com/jsit-dj-it/p/11266815.html