程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python decorator with parameters

編輯:Python
  1. Explanation of decorator with parameters
# !/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()
  1. 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()
  1. 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()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved