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 ...
随机推荐
- Oracle对列的操作总结
1.更改列名 alter table TABLE_NAME rename column COLUMN_OLD COLUMN_NEW; 2.添加列 alter table TABLE_NAME add ...
- 解决Oracle+weblogic系统死机的问题
前段时间发布的系统(Oracle+weblogic)频繁挂掉,每天早上9点.下午2点高峰期就挂,纠结了很长时间,最终解决,方法描述下. 执行select count(*),status from v$ ...
- 常见类型,isset(),empty()判断
<?php $a = NULL; var_dump($a); //NULL,[false],true var_dump(isset($a)); var_dump ...
- 深入理解css系列:meta标签
积累太少,时间管理技巧欠缺,所以导致了博客更新的速度迟缓.学习中成长,成长中学习.加油吧!最近在做h5的项目,对于meta标签层出不穷的各式属性值有点头晕,所以查资料整理了下. 关键字:meta na ...
- easyUI中datetimebox和combobox的取值方法
easyUi页面布局中,查询条件放在JS中,如下 <script type="text/javascript"> var columnList = [ [ { ...
- Linux 防火墙开放特定端口 (iptables)
iptables是linux下的防火墙,同时也是服务名称. service iptables status 查看防火墙状态 service iptables start ...
- HTML table、form表单标签的介绍
1. <table>标签 1.1说明: 在HTML 中定义表格布局. 1.2格式: <table> <caption></caption> <tr ...
- Maven学习-处理资源文件
在前面两篇文章中,我们学习了Maven的基本使用方式和Maven项目的标准目录结构.接下来,我们来看下Maven是如果管理项目中的资源文件的. Java项目的资源文件,主要用于存储系统的配置信息,以及 ...
- mysql 导入导出方法。
1.导出 mysqldump -u username - p databasename >名.sql enter passward: 2.导入:mysql -uroot -proot sour ...
- Java实验三
20145113 20145102实验三 实验步骤 编码标准 编程标准包含:具有说明性的名字.清晰的表达式.直截了当的控制流.可读的代码和注释,以及在追求这些内容时一致地使用某些规则和惯用法的重要性 ...