装饰器:

定义:本质是函数(装饰其它函数) 为其它函数添加附加功能

原则: 1 不能修改被装饰函数源代码    2 不修改被装饰函数调用方式

实现装饰器知识储备:

1 函数即‘’变量‘’

2 高阶函数

  a 把一个函数名当实参传给另外一个函数(在不修改被装饰函数的情况下 为其添加功能)

  b 返回值中包含函数名(不修改函数的调用方式)

3嵌套函数

高阶函数 + 嵌套函数 =》 装饰器

简单版高阶函数

import time
def bar():
time.sleep(3)
print('in the bar')
def test2(func):
print(func)
return func bar=test2(bar)
bar() #run bar

  简单版 嵌套函数

def foo():
print('in the foo')
def bar():
print('in the bar') bar()
foo()

  装饰器 = 高阶函数 + 嵌套函数

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 test1():
time.sleep(3)
print('in the test1') test1()

  修改版装饰器 任意传参数版:

import time
def timer(func): #timer(test1) func=test1
def deco(*args,**kwargs):
start_time=time.time()
func(*args,**kwargs) #run test1()
stop_time = time.time()
print("the func run time is %s" %(stop_time-start_time))
return deco
@timer #test1=timer(test1)
def test1():
time.sleep(1)
print('in the test1') test1()

  修改版装饰器 任意传参数版2

# Author:Lance
import time
def timer(func): #timer(test1) func=test1
def deco(*args,**kwargs):
start_time=time.time()
r =func(*args,**kwargs) #run test1()
stop_time = time.time()
print("the func run time is %s" %(stop_time-start_time))
return r
return deco
@timer #test1=timer(test1)
def test1():
time.sleep(1)
print('in the test1')
return "你是大撒B" @timer # test2 = timer(test2) = deco test2(name) =deco(name)
def test2(name,age):
print("test2:",name,age) a =test1()
print(a)
test2("abc",123) 输出结果:
in the test1
the func run time is 1.0000011920928955
你是大撒B
test2: abc 123
the func run time is 0.0

  加强版 装饰器

import time
user,passwd = 'lance',''
def auth(auth_type):
print("auth func:",auth_type)
def outer_wrapper(func):
def wrapper(*args, **kwargs):
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) # from home
print("---after authenticaion ")
return res
else:
exit("\033[31;1mInvalid username or password\033[0m")
elif auth_type == "ldap":
print("搞毛线ldap,不会。。。。") return wrapper
return outer_wrapper @auth(auth_type="local") # home = wrapper()如果没有语法糖@ 相当于这样的写法home = auth(auth_type='local')(home)
def home():
print("welcome to home page")
return "from home" print(home()) #wrapper() 输出结果
auth func: local
wrapper func args:
Username:lance
Password:123456
User has passed authentication
welcome to home page
---after authenticaion
from home

装饰器更高级的用法:请参照 https://www.cnblogs.com/cicaday/p/python-decorator.html

生成器:略

迭代器:略

python中的装饰器迭代器生成器的更多相关文章

  1. Python中的装饰器,迭代器,生成器

    1. 装饰器 装饰器他人的器具,本身可以是任意可调用对象,被装饰者也可以是任意可调用对象. 强调装饰器的原则:1 不修改被装饰对象的源代码 2 不修改被装饰对象的调用方式 装饰器的目标:在遵循1和2的 ...

  2. 精析python中的装饰器、生成器

    装饰器: 在编程时,要遵循一个原则,就是开放-封闭原则. 在不破坏原函数的情况下,要想对原函数进行一些修饰,那么这里就要用到装饰器. 例如:你完成了一些用函数写成的项目,此时公司正在年度考核,你需要给 ...

  3. 简单说明Python中的装饰器的用法

    简单说明Python中的装饰器的用法 这篇文章主要简单说明了Python中的装饰器的用法,装饰器在Python的进阶学习中非常重要,示例代码基于Python2.x,需要的朋友可以参考下   装饰器对与 ...

  4. 【Python】python中的装饰器——@

    对装饰器本来就一知半解的,今天终于弄清楚了,Python中的装饰器是对装饰者模式的很好运用,简化到骨子里了. python中为什么需要装饰器,看这里:http://www.cnblogs.com/hu ...

  5. Python 中实现装饰器时使用 @functools.wraps 的理由

    Python 中使用装饰器对在运行期对函数进行一些外部功能的扩展.但是在使用过程中,由于装饰器的加入导致解释器认为函数本身发生了改变,在某些情况下——比如测试时——会导致一些问题.Python 通过  ...

  6. 写python中的装饰器

    python中的装饰器主要用于在已有函数实现功能前附加需要输出的信息,下面将用实例展示我如何写装饰器. 首先分别尝试写装饰器装饰一个无参函数和一个有参函数(被装饰函数仅输出,无返回值情况下) def ...

  7. python中的装饰器decorator

    python中的装饰器 装饰器是为了解决以下描述的问题而产生的方法 我们在已有的函数代码的基础上,想要动态的为这个函数增加功能而又不改变原函数的代码 例如有三个函数: def f1(x): retur ...

  8. python中@property装饰器的使用

    目录 python中@property装饰器的使用 1.引出问题 2.初步改善 3.使用@property 4.解析@property 5.总结 python中@property装饰器的使用 1.引出 ...

  9. python装饰器,迭代器,生成器,协程

    python装饰器[1] 首先先明白以下两点 #嵌套函数 def out1(): def inner1(): print(1234) inner1()#当没有加入inner时out()不会打印输出12 ...

随机推荐

  1. MySQL中Identifier Case Sensitivity

    在MySQL当中,有可能遇到表名大小写敏感的问题.其实这个跟平台(操作系统)有关,也跟系统变量lower_case_table_names有关系.下面总结一下,有兴趣可以查看官方文档"Ide ...

  2. 常用SMTP地址

    1.QQ邮箱(mail.qq.com) POP3服务器地址:pop.qq.com(端口:110) SMTP服务器地址:smtp.qq.com(端口:25) 2.搜狐邮箱(sohu.com): POP3 ...

  3. SQL Server查看视图定义总结

      在SQL Server中如何查看数据库视图的定义呢? 其实官方文档已经有一个较详细的总结了,这里在官方文档的基础上,我们再深入展开分析一下,例如如何获取系统视图的定义.知其然知其所以然吗. 1:使 ...

  4. jenkins+gitlab配置

    jenkins配置 插件配置 Jenkins要实现持续集成自动部署需要安装  gitlab  maven Publish Over SSH  Git等几个插件 查看已经安装的插件 jenkins上集成 ...

  5. 【Linux基础】查看某一端口是否开放(1025为例)

    1.使用lsof 命令来查看端口是否开放 lsof -i:1025 //如果有显示说明已经开放了,如果没有显示说明没有开放 lsof(list open files)是一个列出当前系统打开文件的工具. ...

  6. 混合编程[python+cpp+cuda]

    很多时候,我们是基于python进行模型的设计和运行,可是基于python本身的速度问题,使得原生态python代码无法满足生产需求,不过我们可以借助其他编程语言来缓解python开发的性能瓶颈.这里 ...

  7. SpringCloud搭建Eureka集群

    第一部分:搭建Eureka Server集群 Step1:新建工程,引入依赖 依赖文件pom.xml如下 <?xml version="1.0" encoding=" ...

  8. 基于 HTML5 WebGL 的 3D 工控裙房系统

    前言 工业物联网在中国的发展如火如荼,网络基础设施建设,以及工业升级的迫切需要都为工业物联网发展提供了很大的机遇.中国工业物联网企业目前呈现两种发展形式并存状况:一方面是大型通讯.IT企业的布局:一方 ...

  9. 遍历CheckBox根据指定条件做筛选js

    $('#del').click(function(){ var checkeds=$('input[name=cid]:checked') checkeds.each(function() { var ...

  10. 细述:nginx http内核模块提供的变量和解释

    导读 ngx_http_core_module模块在处理请求时,会有大量的变量,这些变量可以通过访问日志来记录下来,也可以用于其它nginx模块. 在我们对请求做策略如改写等等都会使用到一些变量,顺便 ...