Python3中装饰器的使用
较为复杂的装饰器使用:
user,passwd = 'hjc',111111 def auth(type):
print('auth type:',type)
def outwrapper(func):
def wrapper(*args,**kwargs):
if type == 'lll':
username = input('username:').strip()
password = int(input('password:').strip())
if user == username and passwd == password:
print("\033[32;1muser pass ~~~~~~\033[0m")
func(*args,**kwargs)
else:
exit("\033[31;1minvalid user ~~~~~~\033[0m")
elif type == 'kkk':
print('fuck off')
return wrapper
return outwrapper def index():
print("welcome to index page")
@auth('lll')
def home():
print("welcome to home page")
@auth('kkk')
def bbs():
print("welcome to bbs page")
index()
home()
bbs()
输出结果如下:
auth type: lll
auth type: kkk
welcome to index page
username:hjc
password:111111
user pass ~~~~~~
welcome to home page
fuck off
2.较为简单的装饰器:
import time def timer(func):
def inner(*args,**kwargs):
start_time = time.time()
func(*args,**kwargs)
stop_time = time.time()
print(stop_time-start_time)
return inner @timer
def wt():
print('hello wt!!!') wt()
Python3中装饰器的使用的更多相关文章
- python3中装饰器的用法总结
装饰器预备知识点 1 函数赋值给一个变量 函数名可以像普通变量一样赋值给另一个变量. def test(): print("i am just a test function") ...
- (转)Python3.5——装饰器及应用详解
原文:https://blog.csdn.net/loveliuzz/article/details/77853346 Python3.5——装饰器及应用详解(下)----https://blog.c ...
- python3.7 装饰器
#!/usr/bin/env python __author__ = "lrtao2010" #python3.7 装饰器 #装饰器 ''' 定义:本质就是一个函数,作用是为其他函 ...
- 8.Python中装饰器是什么?
Python中装饰器是什么? A Python decorator is a specific change that we make in Python syntax to alter functi ...
- 第7.18节 案例详解:Python类中装饰器@staticmethod定义的静态方法
第7.18节 案例详解:Python类中装饰器@staticmethod定义的静态方法 上节介绍了Python中类的静态方法,本节将结合案例详细说明相关内容. 一. 案例说明 本节定义了类Sta ...
- Python中装饰器的用法
定义: 装饰器本身就是一个函数 为其他函数提供附加功能 不改变源代码 不改变原调用方式 装饰器=高阶函数+嵌套函数 知识点: 函数本身就是一个变量(意味着可以被复制给一个变量:test=test(1) ...
- python flask route中装饰器的使用
问题:route中的装饰器为什么感觉和平时使用的不太一样,装饰器带参数和不太参数有什么区别?被修饰的函数带参数和不带参数有什么区别? 测试1:装饰器不带参数,被修饰的函数也不带参数. def log( ...
- python中装饰器的原理
装饰器这玩意挺有用,当时感觉各种绕,现在终于绕明白了,俺滴个大爷,还是要慢慢思考才能买明白各种的真谛,没事就来绕一绕 def outer(func): def inner(): print(" ...
- python中装饰器修复技术
python装饰器@wraps作用-修复被装饰后的函数名等属性的改变 Python装饰器(decorator)在实现的时候,被装饰后的函数其实已经是另外一个函数了(函数名等函数属性会发生改变), 为了 ...
随机推荐
- Azure的CentOS上安装LIS (Linux Integration Service)
Azure上虚拟化技术都是采用的Hyper-v,每台Linux虚拟机都安装了LIS(Linux Integration Service).LIS的功能是为VM提供各种虚拟设备的驱动.所以LIS直接影响 ...
- java bean Format注解用法
@NumberFormat(style=Style.NUMBER) private int number; @DateTimeFormat(pattern="yyyy-MM-dd&qu ...
- Java-API:javax.servlet.http.HttpServletRequest
ylbtech-Java-API:javax.servlet.http.HttpServletRequest 1.返回顶部 1. javax.servlet.http Interface HttpSe ...
- 二 Istio设计的核心原则
Istio架构关键目标 最大化透明度:Istio将自身自动注入到服务间所有的网络路径中.Istio使用sidecar代理来捕获流量,并且在尽可能的地方自动编程网络层,通过代理来路由流量,无需改动应用程 ...
- 调试json
console.log("======================") // 转对象 //var obj = eval('(' + data + ')'); // 转对象 // ...
- 安装 MongoDB。
1.安装 MongoDB. 1.为软件包管理系统导入公钥. Ubuntu 软件包管理工具为了保证软件包的一致性和可靠性需要用 GPG 密钥检验软件包.使用下列命令导入 MongoDB 的 GPG 密钥 ...
- UIView显示原理和过程
一.UIView显示原理 一个控件,UIView之所以可以显示,是因为内部在UIView的内部有一个layer属性作为根图层,根图层上可以放其他子图层,在UIView中所有能够看到的内 ...
- 没有dig命令的结觉方法
-bash: dig: command not found rpm -qa bind-utils 查看没有没安装 bind-utils 包 yum install -y bind-utils 解决
- Developer tools
20. Developer tools Spring Boot includes an additional set of tools that can make the application de ...
- java飞机大战之子弹的自动生成
import java.awt.Graphics; import java.util.ArrayList; import javax.swing.JFrame; import javax.swing. ...