• Python写一个对象,让它自己能够迭代


    仿写range()对象,对象是可迭代的:

     1 #!usr/bin/env python3
     2 # -*- coding=utf-8 -*-
     3 
     4 class myRange():
     5     #初始化,也叫构造函数
     6     def __init__(self,n):
     7         self.i = 0
     8         self.n = n
     9 
    10     #类中核心,返回了迭代器本身;
    11     def __iter__(self):
    12         return self
    13 
    14     #迭代器开始
    15     def __next__(self):
    16         if self.i < self.n:
    17             i = self.i
    18             self.i += 1
    19             return i
    20         else:
    21             raise StopIteration();
    22 print(dir(myRange(10)));
    23 a = myRange(10);
    24 print(dir(a)); #输出:['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'i', 'n']
    25 
    26 if __name__ == "__main__":
    27     a = myRange(10)
    28     print("a=========>", next(a)); #0
    29     print("next a====>", next(a)); #1
    30     print("next a====>", next(a)); #2
    31     print("next a====>", next(a)); #3
    32     print("next a====>", next(a)); #4
    33     print("next a====>", next(a)); #5
    34     print("next a====>", next(a)); #6
    35     print("next a====>", next(a)); #7
    36     print("next a====>", next(a)); #8
    37     print("next a====>", next(a)); #9
    38     print("next a====>", next(a)); #发起raise StopIteraion()
    39     

     上面是用next()把迭代器的所有元素一个一个依次打印出来的,当然也可以用for循环循环出来. for i in x: print(i),然后再用next(a)就会发起raise StopIteration()。下面用列表尝试一下:

     1 #!usr/bin/env python3
     2 # -*- coding=utf-8 -*-
     3 
     4 class myRange():
     5     #初始化,也叫构造函数
     6     def __init__(self,n):
     7         self.i = 0
     8         self.n = n
     9 
    10     #类中核心,返回了迭代器本身;
    11     def __iter__(self):
    12         return self
    13 
    14     #迭代器开始
    15     def __next__(self):
    16         if self.i < self.n:
    17             i = self.i
    18             self.i += 1
    19             return i
    20         else:
    21             raise StopIteration();
    22 print(dir(myRange(10)));
    23 a = myRange(10);
    24 print(dir(a)); #输出:['__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__next__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'i', 'n']
    25 
    26 if __name__ == "__main__":
    27     b = myRange(10)
    28     print(list(b)) #全部打印出来了,后续没有元素了
    29     print("b===>next b", next(b)) #StopIteration.

    把迭代器里的所有元素装到列表中后,再调用next()也会发起:raise StopIteraion.

  • 相关阅读:
    Spring 控制反转容器(Inversion of Control – IOC)
    理解依赖注入(DI
    创建一个简单的Spring应用
    Spring开发环境搭建(Eclipse)
    Spring框架模块
    Spring 框架介绍
    spring教程
    Bulma CSS
    Bulma CSS
    Bulma CSS
  • 原文地址:https://www.cnblogs.com/mafu/p/13539638.html
Copyright © 2020-2023  润新知