Acquaintance function - decorator

  • Closure: in nested function, i.e. not in the global variable, the function is not in the inner layer and the outer layer must be a variable reference function inner function
  • Open Closed Principle: Extension is open to modifications to the source code is closed ... without modifying source code and invocation of the function it is to be extended for additional open closed principle
  • Decorator

Standard Decorators

warpper DEF (FUNC):
DEF Inner (* args, ** kwargs):
operation before performing the original function # ...
RET = FUNC ()
# operation after execution of the primitive ...
return RET
return Inner

Standard decorators example ---- Analog blog login garden decorator

Automatically jump to the login page when logged-in account can access other pages accessible without login account to access other pages: functions

login_status = {
'username':None,
'flag':False,
}

def auth(func):
def inner(*args,**kwargs):
if login_status['flag']:
ret = func(*args,**kwargs)
return ret
username = input('请输入用户名:').strip()
password = input('请输入密码:').strip()
if username == 'baoyuan' and password == 'bangbangde':
login_status['flag'] = True
ret = func(*args,**kwargs)
return ret
return inner

@auth
DEF Diary ():
Print ( 'Welcome to the diary pages')

def auth(x):
def auth1(func):
def inner(*args,**kwargs):
if dic_flag['flag']:
ret = func()
return ret
if x == 'wechat':
username = input('name')
password = input('pwd')
if username == 'asd' and password == '123':
ret = func()
return ret
if x == 'QQ':
username = input('name')
password = input('pwd')
if username == 'asd' and password == '123':
ret = func()
return ret
return inner
return auth1

@auth ( 'wechat')
DEF the Jitter ():
Print ( 'record better life')

@auth ( 'QQ')
DEF pipefish ():
Print ( 'look forward to your comments connotation of God')

Guess you like

Origin www.cnblogs.com/lingshuai/p/11938948.html