装饰器、生成器,迭代器、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 ...
随机推荐
- Ubuntu 安装 ImageMagic(6.9.1-6)及 PHP 的 imagick (3.0.1)扩展
关于 ImageMagic 和 imagick 的介绍,见<图片处理神器ImageMagick以及PHP的imagick扩展> 和 <Ubuntu下安装ImageMagick和Mag ...
- PHP+jQuery 列表分页类 ( 支持 url 分页 / ajax 分页 )
/* ******* 环境:Apache2.2.8 ( 2.2.17 ) + PHP5.2.6 ( 5.3.3 ) + MySQL5.0.51b ( 5.5.8 ) + jQuery-1.8.3.mi ...
- PHP+jQuery 注册模块的改进之二:激活链接的URL设置与有效期
接<PHP+jQuery 注册模块的改进之一>继续修改: ①在注册成功后返回登录邮件页面( maillogin.php ),在页面中用户可以点击链接跳转到自己注册邮箱的登录页面,可以再次发 ...
- [源码]随机获取虾米音乐song_id API文件
[源码]随机获取虾米音乐song_id API文件 January 11, 2015 注意:此API请放置于国内主机使用,如香港.北京等等,否则会提示:虾米音乐在您所处的国家或地区暂时无法使用 < ...
- 【转载】Http协议
HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出,经过几年的使用与发展,得到不断地完善和扩展.目前在WWW中使用的是HTTP/1.0的第 ...
- Python实用工具包Scrapy安装教程
对于想用每个想用Python开发网络爬虫的开发者来说,Scrapy无疑是一个极好的开源工具.今天安装之后觉得Scrapy的安装确实不易啊.所以在此博文一篇,往后来着少走弯路. 废话不多说了,如果 ...
- Natural Language Processing Computational Linguistics
http://www.nltk.org/book/ch00.html After this, the pace picks up, and we move on to a series of chap ...
- wajueji
#include<stdio.h>int map[3]={42,3,99};int step[3]={0};int max=99999;void qian(){ int i=0; int ...
- ServletContextDemo
1.servlet 之间共享数据 package xw.servlet; import javax.servlet.ServletContext; import javax.servlet.http. ...
- Python之list添加新元素、删除元素、替换元素
Python之list添加新元素 现在,班里有3名同学: >>> L = ['Adam', 'Lisa', 'Bart'] 今天,班里转来一名新同学 Paul,如何把新同学添加到现有 ...