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 ...
随机推荐
- [转载 ]POJ 1273 最大流模板
转载 百度文库花了5分下的 不过确实是自己需要的东西经典的最大流题POJ1273 ——其他练习题 POJ3436 . 题意描述: 现在有m个池塘(从1到m开始编号,1为源点,m为汇点),及n条水渠,给 ...
- Cosh.1
不管 先查壳 是VC++的 拿到OD 看看 经过跟踪 发现一个挺神奇的地方 再随便单步单步看看,发现CreateFileA返回的值总是-1 0040137A 83F8 00 ...
- C# 的各种排序
http://www.cnblogs.com/jiajiayuan/category/302446.html
- dynamic_cast 和 static_cast 隐式类型转换的区别
首先回顾一下C++类型转换: C++类型转换分为:隐式类型转换和显式类型转换 第1部分. 隐式类型转换 又称为“标准转换”,包括以下几种情况:1) 算术转换(Arithmetic conversion ...
- 2014嘉杰信息杯ACM/ICPC湖南程序设计邀请赛暨第六届湘潭市程序设计竞赛
比赛链接: http://202.197.224.59/OnlineJudge2/index.php/Contest/problems/contest_id/36 题目来源: 2014嘉杰信息杯ACM ...
- magento的url中 去掉多余的目录层级
有时我们需要仅仅显示一层目录的URL路径.而不要出现多个路径的现实,我们可以用以下方法修改: Edit /app/code/core/Mage/Catalog/Model/Url.php 找到632 ...
- check_pkg函数解析
函数位置,conf/core,具体代码如下: check_pkg() { # Usage: check_pkg <command> <package> # It means: ...
- redis 数据结构一 之t_string
简介 REDIS有非常丰富的数据结构 以及建立在这数据结构上的操作,在源文件中主要集中在 T_hash.c /T_list.c /T_string.c/T_zset.c 可以说读懂了这4个源文件 大 ...
- 解决getElementsByClassName的兼容性问题
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8&quo ...
- sublime text 2 ubuntu安装及插件管理
参考 dudumao 1.下载Sublime Text2官网下载地址:http://www.sublimetext.com 2.安装Sublime Text2解压即可使用 $ sudo tar -jx ...