Python的平凡之路(4)
for循环的数据类型有以下几种:list、tuple、dict、set、str等;generator,包括生成器和带yield的generator function。for循环的对象统称为可迭代对象:Iterable。isinstance()判断一个对象是否是Utterable对象。next()函数调用并不断返回下一个值的对象称为迭代器:Iterator。Iterator对象,但list、dict、str虽然是Iterable,却不是Iterator。list、dict、str等Iterable变成Iterator可以使用iter()函数:for循环的对象都是Iterable类型; 凡是可作用于next()函数的对象都是Iterator类型,它们表示一个惰性计算的序列;list、dict、str等是Iterable但不是Iterator,不过可以通过iter()函数获得一个Iterator对象。for循环本质上就是通过不断调用next()函数实现的for循环本质上就是通过不断调用next()函数实现的, 对于可迭代对象,for语句可以通过iter()方法获取迭代器,并且通过next()方法获得容器的下一个元素。例如:for x in [1, 2, 3, 4, 5]: pass实际上完全等价于:
it = iter([1, 2, 3, 4, 5])
# 循环:
while True:
try:
# 获得下一个值:
x = next(it)
except StopIteration:
# 遇到StopIteration就退出循环
break
#Author is wspikh
# -*- coding: encoding -*-
#这1亿个数并没有真正生成,调用的时候才生成。
b = (i*2 for i in range(100))
#b.__next__()) 的含义
for i in b:
print(i)
def fib(max):
n, a, b = 0, 0, 1
while n < max:
#print(b)
yield b #返回了函数的中断状态
a, b = b, a + b
n = n + 1
return 'done'
print(fib(100))
f = fib(100)
print(f.__next__())
print("===========")
print(f.__next__())
print(f.__next__())
print(f.__next__())
print(f.__next__())
print(f.__next__())
g = fib(6)
while True:
try:
x = next(g)
print('g:', x)
except StopIteration as e:
print('Generator return value:', e.value)
#Author is wspikh
# -*- coding: encoding -*-
from collections import Iterable
#判断是不是可迭代对象
print(isinstance('abc',Iterable))
print(isinstance('[1,2,3]',Iterable))
print(isinstance('{"server":"192.168.1.10"}',Iterable))
print(isinstance((x for x in range(10)), Iterable))
print(isinstance(100,Iterable))
print("")
print("")
from collections import Iterator
#判断是不是迭代器对象
print(isinstance('abc',Iterator))
print(isinstance('[1,2,3]',Iterator))
print(isinstance('{"server":"192.168.1.10"}',Iterator))
#生成器肯定是迭代器
print(isinstance((x for x in range(10)), Iterator))
#Author is wspikh
print("in the foo")
def bar():
print("in the bar")
bar()
foo()
#Author is wspikh
# -*- coding: encoding -*-
import time
def bar():
time.sleep(4)
print('in the bar')
def test1(func):
#print(func)
start_time = time.time()
func() #run bar
stop_time = time.time()
print("the fund run time is %s" %(start_time-stop_time))
#不能写成test1(bar()),因为bar()代表函数返回值
#Author is wspikh
# -*- coding: encoding -*-
import time
#嵌套函数和高阶函数的融合
def timer(func):
def deco():
start_time=time.time()
#return func()
func()
stop_time=time.time()
def test1():
time.sleep(3)
print("in the test1")
@timer #语法堂 test2=timer(test2)
def test2():
time.sleep(3)
print("in the test2")
test1()
test2()
#Author is wspikh
# -*- coding: encoding -*-
#import json
import pickle
def sayhi(name):
print("hello",name)
sayhi("alex")
info = {
'age':22,
'name':'alex',
'func':sayhi
}
f = open("json.txt","wb")
#f.write(str(info))
f.write(pickle.dumps(info))
f = open("json.txt","rb")
data = pickle.loads(f.read())
print(data)
Foo/|-- bin/
| |-- foo
|
|-- foo/
| |-- tests/
| | |-- __init__.py
| | |-- test_main.py
| |
| |-- __init__.py
| |-- main.py
|
|-- docs/
| |-- conf.py
| |-- abc.rst
|
|-- setup.py
|-- requirements.txt
|-- README
简要解释:
Python的平凡之路(4)的更多相关文章
- Python的平凡之路(8)
(本文是对平凡之路(7)的补充等) 一.动态导入模块 import importlib __import__('import_lib.metaclass') #这是解释器自己内部用的 #importl ...
- Python的平凡之路(20)
(提问复习为主) 一.Django请求的生命周期 武彦涛: 路由系统 -> 视图函数(获取模板+数据=>渲染) -> 字符串返回给用户 二.路由 ...
- Python的平凡之路(19)
一.Django请求生命周期 对于所有的web框架来说本质就是一个socket服务端,浏览器是socket客户端 ...
- Python的平凡之路(18)
一.JS 正则部分 test - 判断字符串是否符合规定的正则rep = /\d+/;rep.test("asdfoiklfasdf89asdfasdf")# truerep ...
- Python的平凡之路(16)
一.HTML+CSS补充 0.常用页面布局 <!DOCTYPE html> <html lang="en"><head> <meta ch ...
- Python的平凡之路(13)
一.Python的paramiko模块介绍 Python 的paramiko模块,该模块和SSH用于连接远程服务器并执行相关操作 SSH client 用于连接远程服务器并执行基本命令 基于用户名和密 ...
- Python的平凡之路(12)
一.数据库介绍 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,每个数据库都有一个或多个不同的API用于创建,访问,管理,搜索和复制所保存的数据.我们也可以将数据存储在文件中,但 ...
- Python的平凡之路(11)
一. rabbitmq 1 进程Queue: 父进程与子进程进行交互,或者同属于同一父进程下多个子进程进行交互 2 队列通信: send1.py #!/usr/bin/env python#Au ...
- Python的平凡之路(10)
异步IO 数据库 队列 缓存 1.Gevent协程 定义:用户态的轻量级线程.协程拥有自己的寄存器上下文和栈.协程调度切换时,将寄存器上下文和栈保存到其他地方,在切回来的时候,恢复先前保存的寄存器上下 ...
- Python的平凡之路(9)
一.Paramiko模块练习 1. Paramiko模块介绍 Paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接 2 .SSHclie ...
随机推荐
- TCMalloc的使用
Windows下: 1. 编译libtcmalloc_minimal,编成静态的动态的都可以. 2. 在链接中设置附加依赖库libtcmalloc_minimal.lib,并且强制符号引用要加上__t ...
- App.config应用的说明
对访问数据库的链接字符串的封装(MS什么都在封装,弄的我们原来越方(弱)便(智)),好吧,你可以解释说可以方便的更改链接只更改配置,而不用动主程序------隔离(隔离--保护:搞过配电的应该不陌生吧 ...
- [课程设计]Scrum 2.4 多鱼点餐系统开发进度(下单一览页面修复)
Scrum 2.4 多鱼点餐系统开发进度 (下单一览页面修复) 1.团队名称:重案组 2.团队目标:长期经营,积累客户充分准备,伺机而行 3.团队口号:矢志不渝,追求完美 4.团队选题:餐厅到店点餐 ...
- 2016年12月23日 星期五 --出埃及记 Exodus 21:18
2016年12月23日 星期五 --出埃及记 Exodus 21:18 "If men quarrel and one hits the other with a stone or with ...
- JavaScript中的prototype使用说明
参考 http://abruzzi.iteye.com/blog/1026125 http://www.jb51.net/article/23052.htm
- EFS解密----未重装系统
一种方法.(手动删除私钥测试通过) 利用软件advanced efs data recovery 二种方法. 前提:在系统未重装或私钥未丢失.两个软件: PsExec和 IceSword.前者是国外 ...
- assigning to 'id<UIGestureRecognizerDelegate> _Nullable' from incompatible
tip:参考 http://stackoverflow.com/questions/9861538/assigning-to-iddelegate-from-incompatible-type-vie ...
- 用Dictionary替换switch case
用switch case处理一个很长的判断,例如56个民族01代表汉族,02代表藏族,03代表壮族...,当传入数字想获取民族名称时就得写56个case,当传入民族获取背后的数字时,又得再写56个ca ...
- List怎么遍历删除元素
public static void main(String[] args) { List<String> list = new ArrayList<String>(); ...
- android ViewGroup事件分发机制
1:事件分销过程 自定义一个LinearLayout,重写dispatchTouchEvent onInterceptTouchEvent onTouchEvent,定义一个按键重写dispathcT ...