然后给大家介绍的是Wrapper(装饰器),使用广泛。python笔试,面试的话也会百分百问到的,基础和中级的知识储备还是必用的。

让我们开始。

先来一些基础相关知识

*args,**kwargs的区别

def function(x,y,*args,**kwargs):
print(type(x))
print(args)
print(kwargs)
print(type(args))
print(type(kwargs)) function(1,2,3,4,5,a=1,b=2,c=3) <type 'int'>
(3, 4, 5)     #*args返回的是数组
{'a': 1, 'c': 3, 'b': 2} #**kwargs返回的字典
<type 'tuple'>
<type 'dict'>

正题开始

固定模板

装饰器:wrapper
模板:
def 装饰器名(func): #def 与 @之后的函数名称一致 调用函数func与ret=func(*args,**kwargs)内部函数一致
def wrapper(*args,**kwargs): #def 与 return 之后的函数名称一致
ret = func(*args,**kwargs)
return ret #return ret 与 ret=func(*args,**kwargs)一致
return wrapper @装饰器名
def foo():
pass
import time

def timmer(fun1):
def wrapper(*args,**kwargs):
start_time=time.time()
res=fun1(*args,**kwargs)
stop_time=time.time()
print('run time is %s' %(stop_time-start_time))
return res
return wrapper @timmer
def foo():
time.sleep(3)
print('from foo') @timmer
def foo1():
time.sleep(5)
print('from foo1') foo()
foo1()

@wraps:避免被装饰函数自身的信息丢失

未添加@wraps

#-*- coding:utf-8 -*-
def decorator(func):
def inner_function():
pass
return inner_function @decorator
def func():
pass print(func.__name__)
#inner_function

添加@wraps

from functools import wraps

def decorator(func):
@wraps(func)
def inner_function():
pass
return inner_function @decorator
def func():
pass print(func.__name__)
#func
#@wraps:避免被装饰函数自身的信息丢失

多个装饰器的同时使用

def makebold(f):
return lambda:'<b>'+f()+'</b>'
def makeitalic(f):
return lambda:'<i>'+f()+'</i>'
def makeitalic1(f):
return lambda:'<strong>'+f()+'</strong>' @makebold
@makeitalic1
@makeitalic
def say():
return 'hello' print(say())
#<b><strong><i>hello</i></strong></b>
#多个装饰器的执行顺序:是从近到远依次执行。

类装饰器

#类装饰器
class Decorator(object):
def __init__(self, f):
self.f = f
def __call__(self):
print("decorator start")
self.f()
print("decorator end") @Decorator
def func():
print("func") func()
'''
decorator start
func
decorator end
'''
import time

def decorator(func):
def wrapper(*args,**kwargs):
start_time = time.time()
func(*args,**kwargs)
end_time = time.time()
print(end_time - start_time)
return wrapper class Method(object): @decorator
def func(self):
time.sleep(0.8) p1 = Method()
p1.func() # 函数调用
'''
0.815999984741
对于类方法来说,都会有一个默认的参数self,它实际表示的是类的一个实例,所以在装饰器的内部函数wrapper也要传入一个参数
- me_instance(任意参数)就表示将类的实例p1传给wrapper,其他的用法都和函数装饰器相同。
'''

较为复杂的多个装饰器

import time

def deco01(func):
def wrapper(*args, **kwargs):
print("this is deco01")
startTime = time.time()
func(*args, **kwargs)
endTime = time.time()
msecs = (endTime - startTime)*1000
print("time is %d ms" %msecs)
print("deco01 end here")
return wrapper def deco02(func):
def wrapper(*args, **kwargs):
print("this is deco02")
func(*args, **kwargs)
print("deco02 end here")
return wrapper @deco01
@deco02
def func(a,b):
print("hello,here is a func for add :")
time.sleep(1)
print("result is %d" %(a+b)) if __name__ == '__main__':
f = func
f(3,4) '''
this is deco01
this is deco02
hello,here is a func for add :
result is 7
deco02 end here
time is 1032 ms
deco01 end here
'''
#多个装饰器执行的顺序就是从最后一个装饰器开始,执行到第一个装饰器,再执行函数本身。

python--Wrapper的更多相关文章

  1. android 自动化测试 ---python wrapper(python 包装)

    关于有道云笔记复制的东西不能直接copy到博客园,可以选择使用txt文件做个媒介 1.appium 2.monkeyrunner 3.uiautomator2 前面两种种方式都要加载androidsd ...

  2. Python框架、库以及软件资源汇总

    转自:http://developer.51cto.com/art/201507/483510.htm 很多来自世界各地的程序员不求回报的写代码为别人造轮子.贡献代码.开发框架.开放源代码使得分散在世 ...

  3. Awesome Python

    Awesome Python  A curated list of awesome Python frameworks, libraries, software and resources. Insp ...

  4. Machine and Deep Learning with Python

    Machine and Deep Learning with Python Education Tutorials and courses Supervised learning superstiti ...

  5. Building Python 2.7.10 with Visual Studio 2010 or 2015 - Google Chrome

    您的浏览器(Chrome 33) 需要更新.该浏览器有诸多安全漏洞,无法显示本网站的所有功能. 了解如何更新浏览器 × p-nand-q.com C++  Python  Programming  L ...

  6. python提取隐含结构的字符串

    当我用Stanford CoreNLP和A Python wrapper for the Java Stanford Core NLP tools(NLP的python调用工具)进行句法分析时,遇到一 ...

  7. Python:渗透测试开源项目

    Python:渗透测试开源项目[源码值得精读] sql注入工具:sqlmap DNS安全监测:DNSRecon 暴力破解测试工具:patator XSS漏洞利用工具:XSSer Web服务器压力测试工 ...

  8. Python金融行业必备工具

    有些国外的平台.社区.博客如果连接无法打开,那说明可能需要"科学"上网 量化交易平台 国内在线量化平台: BigQuant - 你的人工智能量化平台 - 可以无门槛地使用机器学习. ...

  9. python 各种开源库

    测试开发 来源:https://www.jianshu.com/p/ea6f7fb69501 Web UI测试自动化 splinter - web UI测试工具,基于selnium封装. 链接 sel ...

  10. python 三方面库整理

    测试开发 Web UI测试自动化 splinter - web UI测试工具,基于selnium封装. selenium - web UI自动化测试. –推荐 mechanize- Python中有状 ...

随机推荐

  1. Spring Boot + Redis 实现Shiro集群

    为实现Web应用的分布式集群部署,要解决登录session的统一.本文利用shiro做权限控制,redis做session存储,结合spring boot快速配置实现session共享. 1.引入相关 ...

  2. 从0到1构建适配不同端(微信小程序、H5、React-Native 等)的taro + dva应用

    从0到1构建适配不同端(微信小程序.H5.React-Native 等)的taro + dva应用 写在前面 Taro 是一套遵循 React 语法规范的 多端开发 解决方案.现如今市面上端的形态多种 ...

  3. CSS Grid 布局学习笔记

    CSS Grid 布局学习笔记 好久没有写博客了, MDN 上关于 Grid 布局的知识比较零散, 正好根据我这几个月的实践对 CSS Grid 布局做一个总结, 以备查阅. 1. 基础用法 Grid ...

  4. 怎样学习webpack - 走心分享

    很多朋友可能都知道webpack,也见过webpack的代码,但是不明白里面一坨一坨的东西是什么意思,到底有什么用处!我们每个人学习一个新东西可能都会有这个过程,但是我个人觉得webpack可能是最混 ...

  5. 在Markdown中插入不会显示的注释文本

    方法1 <!-- your comment goes here --> 方法2 [//]: <> (This is also a comment.) 原文地址: https:/ ...

  6. 【转载】#437 - Access Interface Members through an Interface Variable

    Onece a class implementation a particular interface, you can interact with the members of the interf ...

  7. 定位webpack文件大小

    之前发现一个神器,记录一下,可以可视化webpack打包的每个js文件大小,这样对我们优化代码是有帮助的,有目标的 https://www.npmjs.com/package/webpack-bund ...

  8. python 3+djanjo 2.0.7简单学习(二)--创建数据库和模型

    我们紧接上次,这里将建立数据库,创建第一个模型提示:这里我们不需要去一直启动,django会在我们ctrl+s的时候自动刷新并启动服务,很方便吧  1.数据库配置 现在,打开 vote_mysite/ ...

  9. Python实现接口测试中的常见四种Post请求数据

    前情: 在日常的接口测试工作中,模拟接口请求通常有两种方法, 利用工具来模拟,比如fiddler,postman,poster,soapUI等 利用代码来模拟,使用到一些网络模块,比如HttpClie ...

  10. P1424 小鱼的航程(改进版)

    题目背景 原来的题目太简单,现改进让小鱼周末也休息,请已经做过重做该题. 题目描述 有一只小鱼,它上午游泳150公里,下午游泳100公里,晚上和周末都休息(实行双休日),假设从周x(1<=x&l ...