Study Notes eleven python

First, review

Ternary operator # 
variable # reception result = result if the condition is true else condition is false condition results
variable # reception result = "true result" else if condition "a false result"

# namespaces and scope
# three kinds : built-in global local
# scope: global local Globals () about locals ()
# Globals () the local variables declared a global
# nonlocal local variables declared in the last upper layer of the partial
# scope chain: variable with small when start his own name to find space
# can not find on the outer layer by layer to find until you find the
# can not find on the error

nested calls and nested function definitions #
#-defined functions inside the function can not be called directly outside
the variable # internal function may be used outside of the
nature of the function name #
# is a string of memory addresses
# can be assigned, can be used as a container element type, arguments and return values -> first-class objects

# closed package: internal function uses a function of the number of external variables
DEF outer ():
a =. 1
DEF inner ():
Print (a)
return inner

I = outer ()
I ()

II decorator
# Decorator formation process: the most simple decor has a universal parameter return value parameter 
# decorator role
# do not want to modify the function is called, but also want to add functionality to the original function before and after
# principle: Open Closed Principle
# syntactic sugar: @
fixed decorator pattern #


Import time
# # Print (the time.time ()) to get the current time #
# # the time.sleep (10)
#
# DEF Timmer (F): a function of decorator #
# def inner ():
# = Start the time.time ()
# RET = F () function to be decorated #
# = the time.time End ()
# Print (End - Start)
# return RET
# Inner return
#
# # syntactic sugar @timmer @ decorator function
# def func (): # was decorated function
# the time.sleep (0.01)
# Print ( 'Hello everyone good boss good colleague')
# return 'Happy New Year'
#
# # FUNC = Timmer (FUNC) # there would not sentence the syntactic sugar
RET FUNC = # () #inner
# Print (RET)

action # decorator ---> do not want to modify the function is called, but also want to add functionality to the original function before and after
# timmer is a decorator, just a function Some decorative

# principle: open closed principle
# opening: extended open
# closed: modification closed
'' '
decorator # decorator with function parameters
def timmer (f): # decorator function
def inner (* args, ** kwargs):
Start = time.time ()
RET = f (* args, ** kwargs) # garnished function
End = time.time ()
Print (End - Start)
return RET
return Inner

@ timmer # syntactic sugar @ decorator function name
def func (a, b): # function being decorated
the time.sleep (0.01)
Print ( 'good boss good colleague Hello everyone', a, b)
'Happy New Year' return
@timmer # syntactic sugar @ decorator function name
def func1 (a):
the time.sleep (0.01)
Print ( 'Hello everyone good boss colleague', A, B)
'Happy New Year' return

# FUNC = Timmer (FUNC)
# RET = FUNC (1,2)
RET FUNC = (. 1, B = 2)
Print (RET)
'' '
"" ""
fixed format # decorators
def wrapper (f): # decorator function, f is the function name to be decorated
def inner (* args, ** kwargs ): # define inside a function
'' before being decorative function to do '' '
RET = f (* args, ** kwargs) # was decorated function
' '' after being decorative function to do '' '
return RET
return Inner # function that returns the name, do not add ()

@wrapper # syntactic sugar @ decorator function name
DEF FUNC (a, b):
the time.sleep (0.01)
Print ( 'good boss good colleague Hello everyone', a, B)
return 'New hair'

ret = func () # performed in the inner wrapper function func and returns the return value of the function in the
print (ret)
"""

Guess you like

Origin www.cnblogs.com/xiuyou/p/11258454.html