定义:装饰器本质是函数,(装饰其他函数)就是为其他函数添加附加功能
原则:1、不能修改被装饰的函数的源代码
2、不能修改装饰的函数的调用方式 实现装饰器知识储备
1函数即变量
2、高阶函数,满足2个条件之一 1、把一个函数名当做实参传给另外一个函数。2、返回值中包含函数名
(1、在不修改被装饰的函数的源代码的情况下,为其添加功能 2、不能修改函数的调用方式)
3、嵌套函数
高阶函数+嵌套函数=》装饰器
python内存回收机制,匿名函授和变量名没有的情况下会被回收
def foo():
print('in the foo')
bar()
def bar():
print('in the bar')
foo()
import time def bar():
time.sleep(0.5)
print('in the bar') def test1(func):
start_time=time.time()
print(func)
time.sleep(0.5)
func()
stop_time=time.time()
print("the func run time is %s"%(stop_time-start_time)) test1(bar)#打印内存地址,调用方式改变了 import time def bar():#定义原始函数
time.sleep(0.5)#停止0.5秒
print('in the bar')#定义过程
def test2(func):#定义装饰器函数
print(func)#定义过程
return func#返回值“func”
print(test2(bar))#打印函数test2在bar中的内存地址
bar=test2(bar)#重新定义变量bar=函数test2以bar为实参
bar()#run bar#运行bar

  

嵌套函数:在一个函数的体内用def去声明一个函数
def foo():
print('in the foo')
def bar():#在一个函数的体内用def去声明一个函数
print('in the bar') bar()
foo()
x=0
def gramdpa():
x=1
def dad():
x=2
def son():
x=3
print(x)
son()
dad()
gramdpa() import time

  

装饰器1——>无参数装饰器(包含高阶\嵌套函数) 要实现的功能是查看“源代码”程序运行了多少时间
def timer(func):#定义一个名为timer的函数,参数为func,timer(test1)把test1的内存地址传给了func
def deco():#声明一个新的函数deco,函数的嵌套,他的作用
start_time=time.time()#开始时间
func()#等于运行run test1 适用无参数的源代码
stop_time=time.time()#结束时间
print('the func run time is %s'%(stop_time-start_time))#打印...
return deco#高阶函数返回值中有函数deco内存地址 @timer
#源代码
def test1(): #定义名为test1函数
time.sleep(1)#等3秒
print('in the test1')#函数过程 打印...
print(timer(test1))
test1=timer(test1)
test1()#--->执行deco

  

装饰器2-->含多个参数(包含高阶\嵌套函数) 要实现的功能是查看“源代码”程序运行了多少时间
import time
def timer(func):#定义一个名为timer的函数,参数为func,timer(test1)把test1的内存地址传给了func
def deco(*args,**kwargs):#声明一个新的函数deco,函数的嵌套,他的作用
start_time=time.time()#开始时间
func(*args,**kwargs)#等于run test1 *args,**kwargs适用任何参数的源代码
stop_time=time.time()#结束时间
print('the func run time is %s'%(stop_time-start_time))#打印...
return deco#高阶函数返回值中有函数deco内存地址 @timer
源代码
def test1(): #定义名为test1函数
time.sleep(1)#等3秒
print('in the test1')#函数过程 打印...
print(timer(test1))
test1=timer(test1)
test1()#--->执行deco @timer
def test2(name,age):#定义名为test2函数,含有name参数
print("test2:",name,age)#打印 ... test1()
test2("dream",22)#对应源代码加入参数才能运行

  

装饰器3--输入用户名密码,(源代码没有返回数据的装饰器)
import time
user,passwd='dream','133456'#默认密码
def auth(func):
def wrapper(*args,**kwargs):
print("wrapper func args:",*args,**kwargs)
username=input("Username:").strip()#移除空格和回车字符
password=input("Password:").strip()
if user==username and passwd==password:#验证用户名和密码
print('\033[32;1mUser has passed authentication\033[0m')
func(*args,**kwargs)#执行源代码
else:
exit('\33[31;1mInvalid username or password\033[0m')#否则退出
return wrapper @auth
def index():
print('welcome to index page')
@auth
def home():
print("welcome to home page") @auth
def bbs():
print("welcome to home page") # index()
# home()
# bbs()

  


#装饰器4--输入用户名密码,源代码含有返回数据的装饰器
import time
user,passwd='dream','133456'#默认密码
def auth(func):
def wrapper(*args,**kwargs):
# print("wrapper func args:",*args,**kwargs)
username=input("Username:").strip()#移除空格和回车字符
password=input("Password:").strip()
if user==username and passwd==password:#验证用户名和密码
print('\033[32;1mUser has passed authentication\033[0m')
res=func(*args,**kwargs)#定义一个变量
return res else:
exit('\33[31;1mInvalid username or password\033[0m')#否则退出
return wrapper @auth
def index():
print('welcome to index page')
@auth
def home():
print("welcome to home page")
return "from home" @auth
def bbs():
print("welcome to home page") # #index()
# print(home())
# #bbs()

  

装饰器5--输入用户名密码,判断本地认证和远程认证
import time
user,passwd='dream','133456'#默认密码
def auth(auth_type):
print("auth func:",auth_type)
def outer_wrapper(func):
def wrapper(*args,**kwargs):#定义wrapper函数带有两个函数
print("wrapper func args:",*args,**kwargs)
if auth_type=='local':
username=input("Username:").strip()#移除空格和回车字符
password=input("Password:").strip()
if user==username and passwd==password:#验证用户名和密码
print('\033[32;1mUser has passed authentication\033[0m')
res=func(*args,**kwargs)#执行源代码
print('---after authenticaion')
return res
else:
exit('\33[31;1mInvalid username or password\033[0m')#否则退出 elif auth_type=="ldap":
print("行不通")
return wrapper return outer_wrapper def index():
print('welcome to index page')
@auth(auth_type="local")#本地认证
def home():
print("welcome to home page")
return "from home"
@auth(auth_type="ldap") #远程认证
def bbs():
print("welcome to home page")
#
#index()
home()
bbs()

  


												

学习python第十三天,函数5 装饰器decorator的更多相关文章

  1. Python学习第四十天函数的装饰器用法

    在软件开发的过程中,要遵循软件的一些原则封装的,不改变原有的代码的基础增加一些需求,python提供了装饰器来扩展函数功能,下面说说函数装饰器用法 def debug(func):      def ...

  2. Python菜鸟之路:Python基础-逼格提升利器:装饰器Decorator

    一.装饰器 装饰器是一个很著名的设计模式,经常被用于有切面需求的场景,较为经典的有插入日志.性能测试.事务处理等. 装饰器是解决这类问题的绝佳设计,有了装饰器,我们就可以抽离出大量函数中与函数功能本身 ...

  3. Python_高阶函数、装饰器(decorator)

    一.变量: Python支持多种数据类型,在计算机内部,可以把任何数据都看成一个“对象”,而变量就是在程序中用来指向这些数据对象的,对变量赋值就是把数据和变量给关联起来. 对变量赋值x = y是把变量 ...

  4. Python基础(闭包函数、装饰器、模块和包)

    闭包函数 格式: def 函数名1(): def 函数名2(): 变量 = 值 return 变量 return 函数名2 func = 函数名1() key = func()

  5. python 基础篇 11 函数进阶----装饰器

    11. 前⽅⾼能-装饰器初识本节主要内容:1. 函数名的运⽤, 第⼀类对象2. 闭包3. 装饰器初识 一:函数名的运用: 函数名是一个变量,但他是一个特殊变量,加上括号可以执行函数. ⼆. 闭包什么是 ...

  6. Python基础(7)闭包函数、装饰器

    一.闭包函数 闭包函数:1.函数内部定义函数,成为内部函数, 2.改内部函数包含对外部作用域,而不是对全局作用域名字的引用 那么该内部函数成为闭包函数 #最简单的无参闭包函数 def func1() ...

  7. python基础之闭包函数和装饰器

    补充:全局变量声明及局部变量引用 python引用变量的顺序: 当前作用域局部变量->外层作用域变量->当前模块中的全局变量->python内置变量 global关键字用来在函数或其 ...

  8. python之集合,函数,装饰器

    本节主要内容如下: 1. set集合 2. 函数 -- 自定义函数 -- 内置函数 3. 装饰器 一. set 集合: 一个无序且不重复的序列. tuple算是list和str的杂合(杂交的都有自己的 ...

  9. python基础之闭包函数与装饰器

    闭包函数: 什么是闭包函数: 闭指的是定义在一个函数内部 包指的是该函数包含对外部作用域(非全局作用域)名字的引用 def counter(): n=0 def incr(): nonlocal n ...

随机推荐

  1. kafka brokers配置参数详解

    基本配置如下: -broker.id-log.dirs-zookeeper.connect Topic-level配置以及其默认值将在下面讨论. Property Default Descriptio ...

  2. Day3 Form表单

    Day3  Form表单 一.form表单 :提交数据    表单在网页中主要负责数据采集功能,它用<form>标签定义.    用户输入的信息都要包含在form标签中,点击提交后,< ...

  3. AngularJS 指令解析(一)

    AngularJS 指令解析(一) 前言 笔者AngularJS接触时间差不多是两年多,虽然这两年多AngularJS版本日新月异,但是笔者的版本是比较老的1.4.3,一方面是自己对这个版本比较熟悉, ...

  4. (九)JavaScript之【JS函数(定义、参数、调用、【闭包】、call(),apply())】[较深,胆小勿进]

    一].定义: /** * 函数表达式 * * Function() 构造函数 * * 函数提升(Hoisting) * JavaScript默认将当前作用域提升到前面去 * Hoisting应用在变量 ...

  5. android打包代码混淆

    android应用打包代码混淆:   1.将project.propertier文件中的proguard.config=proguard-android.txt打开  拷贝指定的文件到应用中 2.更改 ...

  6. python标准库 - socket

    地址簇(address family) socket.AF_UNIX # (UNIX Domain Sockets) socket.AF_INET # ipv4 socket.AF_INET6 # i ...

  7. CentOS 7.0 各版本下载说明 新增Everything版

    CentOS-7.0-1406有很多可供选择的版本,对初学者来说,不知如何选择,下面做简单的介绍: CentOS-7.0-1406-x86_64-DVD.iso 标准安装版,一般下载这个就可以了 Ce ...

  8. Typora使用

    Typora使用 1.介绍 typora是一款不错的软件. 1.1 基本使用 html js css python java vb 1.2 无序列表 a b c d e f 1.3 java代码 pu ...

  9. Selenium入门系列1 打开浏览器访问网页,退出浏览器

    对于功能自动化的理解就是用测试工具替代手工.手工怎么操作的,工具也如何操作. 手工测试:在前置条件下,执行一定的操作步骤>与预期结果对比 功能自动化:在前置条件下,识别对象 >操作对象&g ...

  10. 新建一个controller并指定为默认的方法

    在之前的项目中升级了MVC的DLL导致一开始程序运行时走的controller的有参构造方法变为走无参构造方法,但是该controller没有无参的构造方法,为了强制让程序走有参的构造方法,就在glo ...