python3 装饰器
#Author by Andy
#_*_ coding:utf-8 _*_
#装饰器的原则及构成:
# 原则:
# 1、不能修改被装饰函数的源代码。
# 2、不能修改被装饰函数的调用方式。
# 3、不能改变被装饰函数的执行结果。
# 装饰器对被装饰函数是透明的。
#
# 如何理解装饰器
# 1、函数即“变量”
# 2、高阶函数
# a:把一个函数名作为实参传递给另外一个函数
# b:返回值中包含函数名
# 3、嵌套函数
#
# 在一个函数体内声明另一个函数称为函数的嵌套
#装饰器例子:
import time
# def timmer(func):
# def warpper(*args,**kwargs):
# start_time=time.time()
# func()
# stop_time=time.time()
# print('the func run time is %s'%(stop_time-start_time))
# return warpper
# @timmer
# def func1():
# time.sleep(5)
# print("I'm a test!")
# func1()
##########################################################################
#1、函数即变量
# def func2(x):
# print('i am %s'%x)
# func3()
# def func3():
# print("func3")
# return "func3"
# func2(func3())
##########################################################################
# 2、高阶函数
# a:把一个函数名作为实参传递给另外一个函数
# b:返回值中包含函数名
#定义一个高阶函数func4
# def func4(func):
# print("i am func4")
# func()
# return func
# def test():
# print("i am test")
# func4(test)
# func4(test)()
##########################################################################
#3、嵌套函数的的局部作用域与全局作用域的访问顺序
x=0
def grandpa():
x=1
def dad():
x=2
def son():
x=3
print(x)
son()
dad()
grandpa()
##########################################################################
#给已知函数test1,test2添加计算函数执行时间的功能
#定义装饰器
def timer1(func):
def deco(*args,**kwargs):
start_time=time.time()
res = func(*args,**kwargs)
stop_time=time.time()
print("The func run time is %s"%(stop_time-start_time))
return res
return deco
#定义test1、test2
@timer1
def test1():
time.sleep(3)
print("I am test1")
@timer1
def test2(x):
time.sleep(4)
print("I am test2's args %s" %x)
return "I am test2"
#test1()
#test2("hello")
print(test2("hello"))
############################################################################
#定义一个登录验证的装饰器,且根据不同情况,使用不通的验证方式:local、ldap
user,passwd='andy','123abc'
def auth(auth_type):
def out_wrapper(func):
def wrapper(*args,**kwargs):
if auth_type == 'local':
username=input("Input username:")
password=input("Input password:")
if user==username and passwd == password:
print("Welcome!")
return func(*args,**kwargs)
else:
print("Wrong username or password!")
exit()
elif auth_type == 'ldap':
print("Sorry, ldap isn't work!")
return wrapper
return out_wrapper
def index():
print("welcome to index page")
@auth(auth_type="local")
def home():
print("welcome to home page")
@auth(auth_type="ldap")
def bbs():
print("welcome to bbs page") index()
home()
bbs()
python3 装饰器的更多相关文章
- 净心诀---python3装饰器
python3装饰器 装饰器作用 简单理解:可以为已有函数添加额外功能 例: 已有2个函数如下 def MyFunc1(): print("This is a print function1 ...
- python3 装饰器应用举例
[引子] python 中的装饰器是oop(面向对象编程)设计模式.之装饰器模式的一个应用.由于有语法糖衣的缘故.所以写起来也更加方便 [从一个比较经典的应用场景来讲解装饰器] 有过一定编程经历的工程 ...
- python3 装饰器全解
本章结构: 1.理解装饰器的前提准备 2.装饰器:无参/带参的被装饰函数,无参/带参的装饰函数 3.装饰器的缺点 4.python3的内置装饰器 5.本文参考 理解装饰器的前提:1.所有东西都是对象( ...
- python3 装饰器初识 NLP第三条
还是先抄一条NLP假设... 三,有效果比有道理更重要 光说做法有道理或者正确而不顾是否有效果,是在自欺欺人. 在三赢(我好,人好,世界好)的原则基础上追求效果,比坚持什么是对的更有意义. 说道理 ...
- python3装饰器用法示例
装饰器在编写后台的逻辑时有可能会用到,比方说一个场景:公司的员工想要登录自己公司的考勤记录系统去修改自己的考勤,以前是随便谁都有权限去修改,这样老板不同意了,现在,要在你登录前加一个权限验证的逻辑,如 ...
- python3装饰器
由于函数也是一个对象,而且函数对象可以被赋值给变量,所以,通过变量也能调用该函数. >>> def now(): ... print('2015-3-25') ... >> ...
- python3 装饰器修复技术@wraps到底是什么?
Python 装饰器中的@wraps的作用: 装饰器的作用: 在不改变原有功能代码的基础上,添加额外的功能,如用户验证等 @wraps(view_func)的作用: 不改变使 ...
- Python3装饰器的使用
装饰器 简易装饰器模板 def wrapper(func): def inner(*args,**kwargs): print('主代码前添加的功能') ret=func(*args,**kwargs ...
- python3装饰器-进阶
一.wraps 作用:优化装饰器 from functools import wraps # 导入wraps def wrapper(f): @wraps(f) # wraps的语法糖 def inn ...
随机推荐
- .Net发出图片Request请求
Stream reader = null; //可能是外部链接 if (imgUrl.ToLower().StartsWith("http://")) { var request ...
- cocos2d-x quick 学习 二 Hello world
总算找到问题了. 这几天一直在招问题没有找到原因. 为什么按照文档就不能建立新的项目. 不能建立自己的 hello world 我之前下载的源码文件 quick-cocos2d-x-2.2.5 ...
- 数据库为什么要用B+树结构--MySQL索引结构的实现
原理: http://blog.csdn.net/cangchen/article/details/44818485 http://blog.csdn.net/kennyrose/article/de ...
- sqllite 默认当前日期写法
create table IF NOT EXISTS realpoint(_id integer primary key autoincrement,rountId varchar(50),lng d ...
- Ajax开发中服务端Response的Content-Type
转自http://www.cnblogs.com/hyl8218/archive/2010/03/10/1681484.html ajax开发中在请求服务器端的响应时, 对于每一种返回类型 规范的做法 ...
- Material Design学习
前言: 最为一个用习惯了bootstrap的前端小菜,今天偶然听闻material design 这个从未听闻的前端框架,带着好奇开始了新的尝试,并将bootstrap跟material design ...
- .net 之缓存
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- js计算两个日期的差值
// 获取两个比较值的毫秒数var postman_confirmtime_ms = Date.parse(new Date(data.postman_confirmtime.replace(/-/g ...
- JSON入门教程
尽管有许多宣传关于 XML 如何拥有跨平台,跨语言的优势,然而,除非应用于 Web Services,否则,在普通的 Web 应用中,开发者经常为 XML 的解析伤透了脑筋,无论是服务器端生成或处理 ...
- C#导出csv文件
/// <summary> /// 将DataTable中数据写入到CSV文件中 /// </summary> /// <param name="dt" ...