装饰器、生成器,迭代器、Json & pickle 数据序列化
1、 列表生成器:代码例子
a=[i*2 for i in range(10)]
print(a) 运行效果如下:
D:\python35\python.exe D:/python培训/s14/day4/列表生成式.py
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18] Process finished with exit code 0
2、高阶函数
变量可以指向函数,函数的参数能接受变量,即把一个函数名当做实参传给另外一个函数
返回值中包涵函数名
代码例子:
1 def test():
2 print("int the test")
3
4 def test2(func):
5 print("in the test2")
6 print(func)
7 func()
8
9 test2(test)
10 运行效果如下:
11 D:\python35\python.exe D:/python培训/s14/day4/高阶函数.py
12 in the test2
13 <function test at 0x000000000110E268> #这里是test的内存地址
14 int the test
15
16 Process finished with exit code 0
3、装饰器
装饰器:本质是函数,(装饰其他函数)就是为其他函数添加附加功能
装饰器原则:
a.不能修改被装饰的函数的源代码
b.不能修改被装饰的函数的调用方式
实现装饰器的知识储备:
a、函数即“变量”
b、高阶函数
c、嵌套函数
高阶函数+嵌套函数=====装饰器

高阶函数:
a.把一个函数名当做实参传给另外一个函数
b.返回值中包含函数名
代码例子
import time
def timeer(func):
def warpper():
start_time=time.time()
func()
stop_time=time.time()
print("the fun runn time is %s" %(stop_time-start_time))
return warpper
@timeer
def test1():
time.sleep(3)
print("in the test1") test1()
运行结果如下:
D:\python35\python.exe D:/python培训/s14/day4/装饰器.py
in the test1
the fun runn time is 3.000171661376953 Process finished with exit code 0
带参数的装饰器
import time def timer(func):
def deco(*args,**kwargs):
start_time=time.time()
func(*args,**kwargs)
stop_time=time.time()
print("the func runn time is %s" %(stop_time-start_time))
return deco @timer #test1 = timer(test1)
def test1():
time.sleep(3)
print("in the test1") @timer def test2(name,age):
print("name:%s,age:%s" %(name,age)) test1()
test2("zhaofan",23)
运行结果如下: D:\python35\python.exe D:/python培训/s14/day4/装饰器3.py
in the test1
the func runn time is 3.000171661376953
name:zhaofan,age:23
the func runn time is 0.0 Process finished with exit code 0
终极版的装饰器
import time
user,passwd = "zhaofan",""
def auth(auth_type):
print("auth func:",auth_type)
def outer_wrapper(func):
def wrapper(*args,**kwargs):
if auth_type=="local":
username = input("Username:").strip()
password = input("Password:").strip()
if user == username and passwd== password:
print("\033[32;1mUser has passed authentication\033[0m")
res = func(*args,**kwargs)
print("------after authentication")
return res
else:
exit("\033[31;1mInvalid username or password\033[0m")
elif auth_type=="ldap":
print("没有ldap")
return wrapper
return outer_wrapper def index():
print("welcome to index page")
@auth(auth_type="local")
def home():
print("welcome to home page")
return "from home"
@auth(auth_type="ldap")
def bbs():
print("welcome to bbs page") index()
print(home())
bbs() 运行结果如下:
D:\python35\python.exe D:/python培训/s14/day4/装饰器4.py
auth func: local
auth func: ldap
welcome to index page
Username:zhaofan
Password:123
User has passed authentication
welcome to home page
------after authentication
from home
没有ldap Process finished with exit code 0
4、通过列表生成式,我们可以直接创建一个列表。但是,受到内存限制,列表容量肯定是有限的。而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要访问前面几个元素,那后面绝大多数元素占用的空间都白白浪费了。
所以,如果列表元素可以按照某种算法推算出来,那我们是否可以在循环的过程中不断推算出后续的元素呢?这样就不必创建完整的list,从而节省大量的空间。在Python中,这种一边循环一边计算的机制,称为生成器:generator。
a = [x for x in range(10)]
print(a) g=(x for x in range(10))
print(g)
运行结果如下:
D:\python35\python.exe D:/python培训/s14/day4/生成器.py
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
<generator object <genexpr> at 0x0000000000B01DB0> Process finished with exit code 0
生成器只有一个方法:__next__()
generator保存的是算法,每次调用next(g),就计算出g的下一个元素的值,直到计算到最后一个元素,没有更多的元素时,抛出StopIteration的错误。
g=(x for x in range(10)) for i in g:
print(i) 运行结果如下: D:\python35\python.exe D:/python培训/s14/day4/生成器的调用.py
0
1
2
3
4
5
6
7
8
9 Process finished with exit code 0
5、可以直接作用于for循环的数据类型有以下几种
一类是集合数据类型,如list、tuple、dict、set、str等;
一类是generator,包括生成器和带yield的generator function。
这些可以直接作用于for循环的对象统称为可迭代对象:Iterable。
可以使用isinstance()判断一个对象是否是Iterable对象
可以被next()函数调用并不断返回下一个值的对象称为迭代器:Iterator。
生成器都是Iterator对象,但list、dict、str虽然是Iterable,却不是Iterator。
凡是可作用于for循环的对象都是Iterable类型;
凡是可作用于next()函数的对象都是Iterator类型,它们表示一个惰性计算的序列;
集合数据类型如list、dict、str等是Iterable但不是Iterator,不过可以通过iter()函数获得一个Iterator对象。
6、json和pickle
装饰器、生成器,迭代器、Json & pickle 数据序列化的更多相关文章
- Python自动化 【第四篇】:Python基础-装饰器 生成器 迭代器 Json & pickle
目录: 装饰器 生成器 迭代器 Json & pickle 数据序列化 软件目录结构规范 1. Python装饰器 装饰器:本质是函数,(功能是装饰其它函数)就是为其他函数添加附加功能 原则: ...
- python基础6之迭代器&生成器、json&pickle数据序列化
内容概要: 一.生成器 二.迭代器 三.json&pickle数据序列化 一.生成器generator 在学习生成器之前我们先了解下列表生成式,现在生产一个这样的列表[0,2,4,6,8,10 ...
- 跟着ALEX 学python day4集合 装饰器 生成器 迭代器 json序列化
文档内容学习于 http://www.cnblogs.com/xiaozhiqi/ 装饰器 : 定义: 装饰器 本质是函数,功能是装饰其他函数,就是为其他函数添加附加功能. 原则: 1.不能修改被装 ...
- Python-Day4 Python基础进阶之生成器/迭代器/装饰器/Json & pickle 数据序列化
一.生成器 通过列表生成式,我们可以直接创建一个列表.但是,受到内存限制,列表容量肯定是有限的.而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要访问前面几个元素,那后面 ...
- 迭代器/生成器/装饰器 /Json & pickle 数据序列化
本节内容 迭代器&生成器 装饰器 Json & pickle 数据序列化 软件目录结构规范 作业:ATM项目开发 1.列表生成式,迭代器&生成器 列表生成式 孩子,我现在有个需 ...
- Python之路-python(装饰器、生成器、迭代器、Json & pickle 数据序列化、软件目录结构规范)
装饰器: 首先来认识一下python函数, 定义:本质是函数(功能是装饰其它函数),为其它函数添加附件功能 原则: 1.不能修改被装饰的函数的源代码. 2.不 ...
- day04 装饰器 迭代器&生成器 Json & pickle 数据序列化 内置函数
回顾下上次的内容 转码过程: 先decode 为 Unicode(万国码 ) 然后encode 成需要的格式 3.0 默认是Unicode 不是UTF-8 所以不需要指定 如果非要转为U ...
- 7th,Python基础4——迭代器、生成器、装饰器、Json&pickle数据序列化、软件目录结构规范
1.列表生成式,迭代器&生成器 要求把列表[0,1,2,3,4,5,6,7,8,9]里面的每个值都加1,如何实现? 匿名函数实现: a = map(lambda x:x+1, a) for i ...
- python三大器(装饰器/生成器/迭代器)
1装饰器 1.1基本结构 def 外层函数(参数): def 内层函数(*args,**kwargs); return 参数(*args,**kwargs) return 内层函数 @外层函数 def ...
随机推荐
- ThinkPHP 学习笔记 ( 二 ) 控制器 ( Controller )
/** * ThinkPHP version 3.1.3 * 部署方式:应用部署 * 文内的 http://localhost/ 由实际主机地址代替 */ 入口文件 index.php: <?p ...
- 使用国内镜像源来加速python pypi包的安装
pipy国内镜像目前有: http://pypi.douban.com/ 豆瓣 http://pypi.mirrors.ustc.edu.cn/ 中国科学技术大学 安装时,使用-i参数 pip i ...
- 【翻译】KNACK制作介绍
KNACK 次世代游戏机的性能开发新世界,PlayStation 4首发游戏的舞台幕后 配合PS4的国内首发,作为SCE的第一个游戏发售的本作. 一边加入发挥次世代机机能的表现,设计了谁都可以 ...
- A20板子上的触摸屏设备号变化后解决
- P1092 虫食算 NOIP2002
为了测试stl 30分的暴力写法... #include <bits/stdc++.h> using namespace std; const int maxn = 11; int n; ...
- socketlog
说明 SocketLog适合Ajax调试和API调试, 举一个常见的场景,用SocketLog来做微信调试, 我们在做微信API开发的时候,如果API有bug,微信只提示“改公众账号暂时无法提供服务, ...
- C++省略参数(va_list va_start va_arg va_end)的简单应用
原文参考自:http://www.cnblogs.com/hanyonglu/archive/2011/05/07/2039916.html #include <iostream> #in ...
- oracle变量的定义和使用【转】
在程序中定义变量.常量和参数时,则必须要为它们指定PL/SQL数据类型.在编写PL/SQL程序时,可以使用标量(Scalar)类型.复合(Composite)类型.参照(Reference)类型和LO ...
- ArcGIS API for Silverlight动态标绘的实现
原文:ArcGIS API for Silverlight动态标绘的实现 1.下载2个dll文件,分别是: ArcGISPlotSilverlightAPI.dll 和 Matrix.dll 其下载地 ...
- proxy解析
知其所以然 本文不是教程向,倾向于分析科学上网的一些原理.知其所以然,才能更好地使用工具,也可以创作出自己的工具. 科学上网的工具很多,八仙过海,各显神通,而且综合了各种技术.尝试从以下四个方面来解析 ...