Python入门5
装饰器
装饰器英文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的更多相关文章
- python入门简介
Python前世今生 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC ...
- python入门学习课程推荐
最近在学习自动化,学习过程中,越来越发现coding能力的重要性,不会coding,基本不能开展自动化测试(自动化工具只是辅助). 故:痛定思痛,先花2个星期将python基础知识学习后,再进入自动化 ...
- Python运算符,python入门到精通[五]
运算符用于执行程序代码运算,会针对一个以上操作数项目来进行运算.例如:2+3,其操作数是2和3,而运算符则是“+”.在计算器语言中运算符大致可以分为5种类型:算术运算符.连接运算符.关系运算符.赋值运 ...
- Python基本语法[二],python入门到精通[四]
在上一篇博客Python基本语法,python入门到精通[二]已经为大家简单介绍了一下python的基本语法,上一篇博客的基本语法只是一个预览版的,目的是让大家对python的基本语法有个大概的了解. ...
- Python基本语法,python入门到精通[二]
在上一篇博客Windows搭建python开发环境,python入门到精通[一]我们已经在自己的windows电脑上搭建好了python的开发环境,这篇博客呢我就开始学习一下Python的基本语法.现 ...
- visual studio 2015 搭建python开发环境,python入门到精通[三]
在上一篇博客Windows搭建python开发环境,python入门到精通[一]很多园友提到希望使用visual studio 2013/visual studio 2015 python做demo, ...
- python入门教程链接
python安装 选择 2.7及以上版本 linux: 一般都自带 windows: https://www.python.org/downloads/windows/ mac os: https:/ ...
- Python学习【第二篇】Python入门
Python入门 Hello World程序 在linux下创建一个叫hello.py,并输入 print("Hello World!") 然后执行命令:python hello. ...
- python入门练习题1
常见python入门练习题 1.执行python脚本的两种方法 第一种:给python脚本一个可执行的权限,进入到当前存放python程序的目录,给一个x可执行权限,如:有一个homework.py文 ...
- Python入门版
一.前言 陆陆续续学习Python已经近半年时间了,感觉到Python的强大之外,也深刻体会到Python的艺术.哲学.曾经的约定,到现在才兑现,其中不乏有很多懈怠,狼狈. Python入门关于Pyt ...
随机推荐
- The first day to learn Englisht
IF you want to go fast,to alone. IF you want to go far,go with others.
- Ubuntu 手工挂载硬盘
首先我们得到到/dev/sda3这个分区的UUID,使用以下命令: sudo blkid /dev/sda3 结果如下: 然后,我们按照/etc/fstab文件中的格式添加一行如下内容: UUID=9 ...
- 解决iOS9下隐藏App返回按钮文字导致的诡异闪屏问题
问题的原因竟是一行代码导致的,这行代码的作用是隐藏App返回按钮的文字. 看看这有问题的代码: //将返回按钮的文字position设置不在屏幕上显示 [[UIBarButtonItem appear ...
- LayaAir引擎——(十)
var k = new Array(); var l = new Array(); var m = new Array(); var zhiyeCurosr = 0; function zyinit( ...
- 2014嘉杰信息杯ACM/ICPC湖南程序设计邀请赛暨第六届湘潭市程序设计竞赛
比赛链接: http://202.197.224.59/OnlineJudge2/index.php/Contest/problems/contest_id/36 题目来源: 2014嘉杰信息杯ACM ...
- 第七章 LED 将为我闪烁:控制发光二级管
在上一章中了解到驱动程序的开发步骤,并一个实列来演示如何开发一个完整的驱动.但这个驱动只是简单的演示了实现步骤.真正的驱动需要与硬件直接进行相互交互.这节完整的演示驱动程序,控制开发板上的4个led灯 ...
- Android 媒体存储服务(二)
Android 媒体存储服务 简介: 本文是<深入Android媒体存储服务>系列第二篇,简要介绍媒体存储服务扫描文件的流程.文中介绍的是 Android 4.2. Android 有一套 ...
- New XAMPP security concept:错误解决方法
New XAMPP security concept:错误解决方法 (2014-03-06 16:07:46) 转载▼ 分类: php 在Linux上配置xampp后远程访问域名报错: New X ...
- 行列式计算(C#)
最近几天学习高等代数老师说要写个程序算行列式的结果,闲来无事就简单写了一下. 不多说了,上代码 using System; using System.Collections.Generic; usin ...
- jquery/js分割数组
substr(star[,length]);//star起始位置 length截取的长度 substring(star,end);//star起始位置,end结束位置 都是以0开始的索引值