-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiter_object.py
More file actions
65 lines (52 loc) · 1.02 KB
/
iter_object.py
File metadata and controls
65 lines (52 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# -*- coding: utf-8 -*-
# 类对象的迭代器
# 生成器示例
# 将迭代器转换成list
' a super function test module '
__author__ = 'lmm'
# class Fibs:
# def __init__(self):
# self.a = 0
# self.b = 1
# def next(self):
# self.a,self.b,self.a+self.b
# def __iter__(self):
# return self
# fibs = Fibs()
# for f in fibs:
# if f > 10:
# print f
# break
def func(nested):
for sublist in nested:
for ele in sublist:
# print ele
yield ele
L = [[1,2],[3,4],[5]]
func(L)
def ood():
print 'step 1'
yield (1)
print 'step 2'
yield (2)
print 'step 3'
yield (3)
d = ood()
for i in d:
print i
# next(d)
# next(d)
# next(d)
# 将迭代器转换成list
class TestIter:
val = 0
def next(self):
self.val += 1
if self.val > 10:
raise StopIteration
return self.val
def __iter__(self):
return self
test_iter = TestIter()
a = list(test_iter)
print a