# !/usr/bin/env python
# -*- coding:utf-8 -*-
# 1、 Parameter decorator
def wrapper_out(parameter):
print(parameter)
def wrapper(func):
def inner(*args, **kwargs):
ret = func(*args, **kwargs)
return ret
return inner
return wrapper
""" @wrapper_out(' penguin ') Analysis and interpretation : When the function executes to the decorator with parameters @wrapper_out(' penguin ') In this sentence , In two steps : 1. perform wrapper_out(' penguin ') This function , Set the corresponding parameters ' WeChat ' Pass to parameter, And get the return value wrapper Function name 2. take @ And wrapper combination , Get the standard version of the decorator we were familiar with , Follow the implementation process of the decorator . """
@wrapper_out(' penguin ')
def qq():
print(' Login Penguin successfully !')
qq()
Application with parameter decorator
# 2、 Decorator application with parameters :
""" Two different software, wechat and Taobao , There are different accounts and passwords , According to the login of different software , Write a decorator to verify the login function Account file name :wechat, Account albert|123 Account file name :taobao, Account don|456 """
def wrapper_out(parameter):
def wrapper(func):
def inner(*args, **kwargs):
if parameter == 'wechat':
username = input(' Please enter a user name :').strip()
password = input(' Please input a password :').strip()
with open('wechat', encoding='utf-8', mode='r') as f:
for line in f:
user, pwd = line.strip().split('|')
if user == username and pwd == password:
ret = func(*args, **kwargs)
return ret
return False
elif parameter == 'taobao':
username = input(' Please enter a user name :').strip()
password = input(' Please input a password :').strip()
with open('taobao', encoding='utf-8', mode='r') as f:
for line in f:
user, pwd = line.strip().split('|')
if user == username and pwd == password:
ret = func(*args, **kwargs)
return ret
return False
return inner
return wrapper
@wrapper_out('wechat')
def wechat():
print(' Successfully log in to wechat !')
wechat()
@wrapper_out('taobao')
def taobao():
print(" Successfully logged in to Taobao !!!")
taobao()
Final version of decorator application with parameters :( Optimized version )
# 3、 Decorator application with parameters : Optimized version
""" Two different software, wechat and Taobao , There are different accounts and passwords , According to the login of different software , Write a decorator to verify the login function Account file name :wechat, Account albert|123 Account file name :taobao, Account don|456 """
def wrapper_out(parameter):
def wrapper(func):
def inner(*args, **kwargs):
username = input(' Please enter a user name :').strip()
password = input(' Please input a password :').strip()
# Note that the file name should be consistent with the parameters passed in by the decorator , The idea of enhancing coupling
with open(parameter, encoding='utf-8', mode='r') as f:
for line in f:
user, pwd = line.strip().split('|')
if user == username and pwd == password:
ret = func(*args, **kwargs)
return ret
return False
return inner
return wrapper
@wrapper_out('wechat')
def wechat():
print(' Successfully log in to wechat !')
wechat()
@wrapper_out('taobao')
def taobao():
print(" Successfully logged in to Taobao !!!")
taobao()