装饰器

装饰器英文Decorator,自身是一个函数,用来包装其它的函数,实现在不改变原来代码的情况下,增加新的功能,返回一个修改后的函数对象,

装饰器功能:

1、装饰器也是函数

2、在不改变原有代码的情况下,增加新的功能

3、符合开放-封闭原则

在学习装饰器之前,我们复习一下函数的相关概念

理解函数也是变量

当我们执行函数不加括号的时候,输出函数的内存地址:

def foo():
print('hello') print(foo) # 输出
<function foo at 0x0000000002CBCBF8>

当我们加上括号后,输出

def foo():
print('hello') foo() # 输出
hello

高阶函数:

1、把一个函数名当做实参传给另外一个函数

def bar():
print('bar') def t1(func):
func() t1(bar) # 输出
bar

2、返回值中包含函数名

def bar():
print('bar') def t1(func):
return func print(t1(bar)) # 输出
<function bar at 0x000000000303CBF8>

函数嵌套

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

前面做了那么多的铺垫,都是为了后边的装饰器,装饰器的组成离不开高阶函数+函数嵌套

无参数装饰器

def logger(func):
def inner():
print('logger start')
res = func()
print('logger stop')
return res return inner @logger
def test1():
print('test1') def test2():
print('test2') test1()
test2() # 输出
logger start
test1
logger stop
test2

带固定参数装饰器

def logger(func):
def inner(arg):
print('logger start')
res = func(arg)
print('logger stop')
return res return inner @logger
def test1():
print('test1') @logger
def test2(name):
print('test2', name) # test1()
test2('chen') # 输出
logger start
test2 chen
logger stop

但是这个时候我的test1函数不能调用了,因为它没有参数,怎么解决,让test1没有参数,test2带参数都可以是用呢?

非固定参数装饰器

def logger(func):
def inner(*args, **kwargs):
print('logger start')
res = func(*args, **kwargs)
print('logger stop')
return res return inner @logger
def test1():
print('test1') @logger
def test2(name):
print('test2', name) test1()
test2('chen')

终极版

def logger(write_type):
# print(write_type)
def outer_wrapper(func):
def inner(*args, **kwargs):
if write_type == 'file':
print('logger start')
res = func(*args, **kwargs)
print('logger stop')
return res
elif write_type == 'db':
print('no support db')
return inner return outer_wrapper @logger(write_type='file')
def test1():
print('test1')
return 'return test1' @logger(write_type='db')
def test2(name):
print('test2', name) a = test1()
print(a)
test2('chen') # 输出
logger start
test1
logger stop
return test1
no support db

生成器

1、生成器只有在调用时候,才会生成相应的数据

2、

json和pickle

json于pickle的区别:

1、json是所有语言通用

2、json只能操作基本数据类型,比如字典、列表、元祖等

3、pickle只能在python内使用

4、pickle可以序列化python内的所有类型

Python入门5的更多相关文章

  1. python入门简介

    Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC ...

  2. python入门学习课程推荐

    最近在学习自动化,学习过程中,越来越发现coding能力的重要性,不会coding,基本不能开展自动化测试(自动化工具只是辅助). 故:痛定思痛,先花2个星期将python基础知识学习后,再进入自动化 ...

  3. Python运算符,python入门到精通[五]

    运算符用于执行程序代码运算,会针对一个以上操作数项目来进行运算.例如:2+3,其操作数是2和3,而运算符则是“+”.在计算器语言中运算符大致可以分为5种类型:算术运算符.连接运算符.关系运算符.赋值运 ...

  4. Python基本语法[二],python入门到精通[四]

    在上一篇博客Python基本语法,python入门到精通[二]已经为大家简单介绍了一下python的基本语法,上一篇博客的基本语法只是一个预览版的,目的是让大家对python的基本语法有个大概的了解. ...

  5. Python基本语法,python入门到精通[二]

    在上一篇博客Windows搭建python开发环境,python入门到精通[一]我们已经在自己的windows电脑上搭建好了python的开发环境,这篇博客呢我就开始学习一下Python的基本语法.现 ...

  6. visual studio 2015 搭建python开发环境,python入门到精通[三]

    在上一篇博客Windows搭建python开发环境,python入门到精通[一]很多园友提到希望使用visual studio 2013/visual studio 2015 python做demo, ...

  7. python入门教程链接

    python安装 选择 2.7及以上版本 linux: 一般都自带 windows: https://www.python.org/downloads/windows/ mac os: https:/ ...

  8. Python学习【第二篇】Python入门

    Python入门 Hello World程序 在linux下创建一个叫hello.py,并输入 print("Hello World!") 然后执行命令:python hello. ...

  9. python入门练习题1

    常见python入门练习题 1.执行python脚本的两种方法 第一种:给python脚本一个可执行的权限,进入到当前存放python程序的目录,给一个x可执行权限,如:有一个homework.py文 ...

  10. Python入门版

    一.前言 陆陆续续学习Python已经近半年时间了,感觉到Python的强大之外,也深刻体会到Python的艺术.哲学.曾经的约定,到现在才兑现,其中不乏有很多懈怠,狼狈. Python入门关于Pyt ...

随机推荐

  1. 用VC调用EXCEL简单代码(转载自越长大越孤单,觉得很好)

    首先在stdafx.h里加入对IDispatch接口提供支持的头文件: #include <afxDisp.h> 再在应用程序类的InitInstance()函数里加入: AfxOleIn ...

  2. java 接口

    1.接口的引出:发现没有继承关系的类也能共享行为 2.接口不是类,类描述对象的属性和行为,但是接口只关注实现的行为3.当我们发现有行为在多个没有继承关系的类中共享,我们要把它抽取到接口中,而不是写到父 ...

  3. Java多线程(转)

    文章转自http://286.iteye.com/blog/2292038 谢谢博主的总结! 进程(Process)是计算机中的程序关于某数据集合上的一次运行活动,是系统进行资源分配和调度的基本单位, ...

  4. EasyUI 开发笔记(一)

    由于某些原因,在公司做的后台需要改成类似于Ext.js 形式的后台,主要看好其中的 框架布局,以及tab开页面和弹出式内部窗体. 后来看看,改成EasyUI,较Ext.js 库小很多,也便于公司的初级 ...

  5. [GodLove]Wine93 Tarining Round #9

    比赛链接: http://vjudge.net/contest/view.action?cid=48069#overview 题目来源: lrj训练指南---二维几何计算   ID Title Pro ...

  6. css3 filter属性在项目中的应用

    css3 属性filter应用在项目里. 语法: <filter>: 要使用的滤镜效果.多个滤镜之间用空格隔开. 设置或检索对象所应用的滤镜效果. 最常用的滤镜效果是不透明效果,如果要实现 ...

  7. eclipse maven plugin 插件 安装 和 配置

      离线插件 点击下载离线安装包:eclipse-maven-plugin.zip ( for eclipse helios or higher ) .解压缩到任意目录(如这里的plugins目录): ...

  8. Linux chroot 并使用之前系统设备节点

    /********************************************************************************* * Linux chroot 并使 ...

  9. android studio sdk 配置

    android studio在启动后会一直处于 fetching Android sdk compoment information 状态 解决办法: 按照网友提供的方法: 第一步: 1)进入刚安装的 ...

  10. Kafka Topic ISR不全,个别Spark task处理时间长

    现象 Spark streaming读kafka数据做业务处理时,同一个stage的task,有个别task的运行时间比多数task时间都长,造成业务延迟增大. 查看业务对应的topic发现当topi ...