|
1
2
3
4
5
6
|
def fab(max):
n, a, b = 0, 0, 1
while n < max:
print b
a, b = b, a + b
n = n + 1
|
|
1
2
3
4
5
6
|
>>> fab(5)
1
1
2
3
5
|
|
1
2
3
4
5
6
7
8
|
def fab(max):
n, a, b = 0, 0, 1
L = []
while n < max:
L.append(b)
a, b = b, a + b
n = n + 1
return L
|
|
1
2
3
4
5
6
7
8
|
>>> for n in fab(5):
… print n
…
1
1
2
3
5
|
|
1
|
for i in range(1000): pass
|
|
1
|
for i in xrange(1000): pass
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
class Fab(object):
def __init__(self, max):
self.max = max
self.n, self.a, self.b = 0, 0, 1
def __iter__(self):
return self
def next(self):
if self.n < self.max:
r = self.b
self.a, self.b = self.b, self.a + self.b
self.n = self.n + 1
return r
raise StopIteration()
|
|
1
2
3
4
5
6
7
8
|
>>> for n in Fab(5):
… print n
…
1
1
2
3
5
|
|
1
2
3
4
5
6
7
8
9
|
def fab(max):
n, a, b = 0, 0, 1
while n < max:
yield b
# print b
a, b = b, a + b
n = n + 1
”’
|
|
1
2
3
4
5
6
7
8
|
>>> for n in fab(5):
… print n
…
1
1
2
3
5
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
>>> f = fab(5)
>>> f.next()
1
>>> f.next()
1
>>> f.next()
2
>>> f.next()
3
>>> f.next()
5
>>> f.next()
Traceback (most recent call last):
File ”
“, line 1, in
StopIteration
|
|
1
2
3
|
>>> from inspect import isgeneratorfunction
>>> isgeneratorfunction(fab)
True
|
|
1
2
3
4
5
|
>>> import types
>>> isinstance(fab, types.GeneratorType)
False
>>> isinstance(fab(5), types.GeneratorType)
True
|
|
1
2
3
4
5
|
>>> from collections import Iterable
>>> isinstance(fab, Iterable)
False
>>> isinstance(fab(5), Iterable)
True
|
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
>>> f1 = fab(3)
>>> f2 = fab(5)
>>> print ‘f1:’, f1.next()
f1: 1
>>> print ‘f2:’, f2.next()
f2: 1
>>> print ‘f1:’, f1.next()
f1: 1
>>> print ‘f2:’, f2.next()
f2: 1
>>> print ‘f1:’, f1.next()
f1: 2
>>> print ‘f2:’, f2.next()
f2: 2
>>> print ‘f2:’, f2.next()
f2: 3
>>> print ‘f2:’, f2.next()
f2: 5
|
|
1
2
3
4
5
6
7
8
9
|
def read_file(fpath):
BLOCK_SIZE = 1024
with open(fpath, ‘rb’) as f:
while True:
block = f.read(BLOCK_SIZE)
if block:
yield block
else:
return
|
发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/207081.html原文链接:https://javaforall.net
