python基础----__next__和__iter__实现迭代器协议

#_*_coding:utf-8_*_
__author__ = 'Linhaifeng'
class Foo:
    def __init__(self,x):
        self.x=x

    def __iter__(self):
        return self

    def __next__(self):
        n=self.x
        self.x+=1
        return self.x

f=Foo(3)
for i in f:
    print(i)

简单示范
简单示范
class Foo:
    def __init__(self,start,stop):
        self.num=start
        self.stop=stop
    def __iter__(self):
        return self
    def __next__(self):
        if self.num >= self.stop:
            raise StopIteration
        n=self.num
        self.num+=1
        return n

f=Foo(1,5)
from collections import Iterable,Iterator
print(isinstance(f,Iterator))

for i in Foo(1,5):
    print(i)
View Code
class Fib:
    def __init__(self):
        self._a=0
        self._b=1

    def __iter__(self):
        return self

    def __next__(self):
        self._a,self._b=self._b,self._a + self._b
        return self._a

f1=Fib()

print(f1.__next__())
print(next(f1))
print(next(f1))

for i in f1:
    if i > 100:
        break
    print('%s ' %i,end='')

斐波那契数列
斐波那契数列

笔记:

  1 from collections import Iterable,Iterator
  2 class Foo:
  3     def __init__(self,start):
  4         self.start=start
  5 
  6     def __iter__(self):
  7         return self
  8 
  9     def __next__(self):
 10         return 'aSB'
 11 
 12 
 13 f=Foo(0)
 14 f.__iter__()
 15 f.__next__()
 16 
 17 print(isinstance(f,Iterable))
 18 print(isinstance(f,Iterator))
 19 
 20 print(next(f)) #f.__next__()
 21 print(next(f)) #f.__next__()
 22 print(next(f)) #f.__next__()
 23 
 24 
 25 for i in f: # res=f.__iter__() #next(res)
 26     print(i)
 27 
 28 
 29 
 30 from collections import Iterable,Iterator
 31 class Foo:
 32     def __init__(self,start):
 33         self.start=start
 34 
 35     def __iter__(self):
 36         return self
 37 
 38     def __next__(self):
 39         if self.start > 10:
 40             raise StopIteration
 41         n=self.start
 42         self.start+=1
 43         return n
 44 
 45 f=Foo(0)
 46 
 47 print(next(f))
 48 print(next(f))
 49 print(next(f))
 50 print(next(f))
 51 print(next(f))
 52 print(next(f))
 53 print(next(f))
 54 print(next(f))
 55 print(next(f))
 56 print(next(f))
 57 print(next(f))
 58 print(next(f))
 59 
 60 for i in f:
 61     print('====>',i)
 62 
 63 
 64 
 65 
 66 class Range:
 67     '123'
 68     def __init__(self,start,end):
 69         self.start=start
 70         self.end=end
 71 
 72     def __iter__(self):
 73         return self
 74 
 75     def __next__(self):
 76         if self.start == self.end:
 77             raise StopIteration
 78         n=self.start
 79         self.start+=1
 80         return n
 81 
 82 for i in Range(0,3):
 83     print(i)
 84 
 85 print(Range.__doc__)
 86 
 87 
 88 class Foo:
 89     '我是描述信息'
 90     pass
 91 
 92 class Bar(Foo):
 93     pass
 94 print(Bar.__doc__) #该属性无法继承给子类
 95 
 96 b=Bar()
 97 print(b.__class__)
 98 print(b.__module__)
 99 print(Foo.__module__)
100 print(Foo.__class__) #?
笔记

 

posted @ 2017-04-25 17:04  ForeverPine  阅读(203)  评论(0编辑  收藏  举报
TOP