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
简要解释: