利用Python 手写一个迭代器

Python
# coding:utf-8 __author__ = 'songhao' class Data(object): def __init__(self): self._data = [] def add(self,a): self._data.append(a) def data(self): return iter(self._data) if __name__ == '__main__': d = Data() d.add(1) d.add(2) d.add(3) for i in d.data(): print(i) """ 运算结果 /usr/local/bin/python3 /Users/songhao/Desktop/Old_Boy_Python/04/data.py 1 2 3 """
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
# coding:utf-8
__author__ = 'songhao'
 
 
class Data ( object ) :
     def __init__ ( self ) :
         self . _data = [ ]
 
     def add ( self , a ) :
         self . _data . append ( a )
 
     def data ( self ) :
         return iter ( self . _data )
 
 
if __name__ == '__main__' :
     d = Data ( )
     d . add ( 1 )
     d . add ( 2 )
     d . add ( 3 )
     for i in d . data ( ) :
         print ( i )
 
"""
运算结果
/usr/local/bin/python3 /Users/songhao/Desktop/Old_Boy_Python/04/data.py
1
2
3
"""



你可能感兴趣的:(利用Python 手写一个迭代器)