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循环的数据类型有以下几种

一类是集合数据类型,如listtupledictsetstr等;

一类是generator,包括生成器和带yield的generator function。

这些可以直接作用于for循环的对象统称为可迭代对象:Iterable

可以使用isinstance()判断一个对象是否是Iterable对象

可以被next()函数调用并不断返回下一个值的对象称为迭代器:Iterator

生成器都是Iterator对象,但listdictstr虽然是Iterable,却不是Iterator

凡是可作用于for循环的对象都是Iterable类型;

凡是可作用于next()函数的对象都是Iterator类型,它们表示一个惰性计算的序列;

集合数据类型如listdictstr等是Iterable但不是Iterator,不过可以通过iter()函数获得一个Iterator对象。

6、json和pickle

装饰器、生成器,迭代器、Json & pickle 数据序列化的更多相关文章

  1. Python自动化 【第四篇】:Python基础-装饰器 生成器 迭代器 Json & pickle

    目录: 装饰器 生成器 迭代器 Json & pickle 数据序列化 软件目录结构规范 1. Python装饰器 装饰器:本质是函数,(功能是装饰其它函数)就是为其他函数添加附加功能 原则: ...

  2. python基础6之迭代器&生成器、json&pickle数据序列化

    内容概要: 一.生成器 二.迭代器 三.json&pickle数据序列化 一.生成器generator 在学习生成器之前我们先了解下列表生成式,现在生产一个这样的列表[0,2,4,6,8,10 ...

  3. 跟着ALEX 学python day4集合 装饰器 生成器 迭代器 json序列化

    文档内容学习于 http://www.cnblogs.com/xiaozhiqi/  装饰器 : 定义: 装饰器 本质是函数,功能是装饰其他函数,就是为其他函数添加附加功能. 原则: 1.不能修改被装 ...

  4. Python-Day4 Python基础进阶之生成器/迭代器/装饰器/Json & pickle 数据序列化

    一.生成器 通过列表生成式,我们可以直接创建一个列表.但是,受到内存限制,列表容量肯定是有限的.而且,创建一个包含100万个元素的列表,不仅占用很大的存储空间,如果我们仅仅需要访问前面几个元素,那后面 ...

  5. 迭代器/生成器/装饰器 /Json & pickle 数据序列化

    本节内容 迭代器&生成器 装饰器 Json & pickle 数据序列化 软件目录结构规范 作业:ATM项目开发 1.列表生成式,迭代器&生成器 列表生成式 孩子,我现在有个需 ...

  6. Python之路-python(装饰器、生成器、迭代器、Json & pickle 数据序列化、软件目录结构规范)

    装饰器: 首先来认识一下python函数, 定义:本质是函数(功能是装饰其它函数),为其它函数添加附件功能        原则:        1.不能修改被装饰的函数的源代码.        2.不 ...

  7. day04 装饰器 迭代器&生成器 Json & pickle 数据序列化 内置函数

    回顾下上次的内容 转码过程: 先decode  为 Unicode(万国码 ) 然后encode 成需要的格式     3.0 默认是Unicode  不是UTF-8 所以不需要指定  如果非要转为U ...

  8. 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 ...

  9. python三大器(装饰器/生成器/迭代器)

    1装饰器 1.1基本结构 def 外层函数(参数): def 内层函数(*args,**kwargs); return 参数(*args,**kwargs) return 内层函数 @外层函数 def ...

随机推荐

  1. 12. 求简单交错序列前N项和

    求简单交错序列前N项和 #include <stdio.h> int main() { int denominator, flag, i, n; double item, sum; whi ...

  2. php常用Stream函数集介绍

    php常用Stream函数集介绍 作者: 字体:[增加 减小] 类型:转载 时间:2013-06-24   本篇文章是对php中的常用Stream函数集进行了详细的分析介绍,需要的朋友参考下     ...

  3. 详解在bash脚本中如何获取自身路径

    DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 这是stac ...

  4. linux下php-fpm 启动参数及重要配置

    约定几个目录 /usr/local/php/sbin/php-fpm/usr/local/php/etc/php-fpm.conf/usr/local/php/etc/php.iniI. php-fp ...

  5. git 第一次初始化

    Command line instructions Git global setup git config --global user.name "{名字}({工号})" git ...

  6. (转)CAS (4) —— CAS浏览器SSO访问顺序图详解(CAS Web Flow Diagram by Example)

    CAS (4) —— CAS浏览器SSO访问顺序图详解(CAS Web Flow Diagram by Example) tomcat版本: tomcat-8.0.29 jdk版本: jdk1.8.0 ...

  7. 500Internal Server Error

    在今晚测试wamp的项目时,当我导入一个项目到www目录下时出现如下错误:Internal Server ErrorThe server encountered an internal error o ...

  8. 那些证书相关的玩意儿(SSL,X.509,PEM,DER,CRT,CER,KEY,CSR,P12等)[zz]

    openssl dgst –sign privatekey.pem –sha1 –keyform PEM –c c:\server.pem 将文件用sha1摘要,并用privatekey.pem中的私 ...

  9. 在脚本中操作plist文件

    终端输入: /usr/libexec/PlistBuddy -c "Print CFBundleIdentifier" /Users/achen/Desktop/testBundl ...

  10. 在大数据中,关于native包的编译步骤

    一.问题的由来: 二.解决问题的方法(所有的操作在root下完成): 1.前期需要的环境,下面的已经在伪分布式中配置好,不再重复 配置好jdk 配置好hadoop 2.上传还需要包 apache-ma ...