python迭代器理解

python迭代器理解

# -*- coding:UTF-8 -*-
from collections import Iterable,Iterator

class Classmate(object):
    def __init__(self):
        self.name = list()
    def __add__(self, name):
        self.name.append(name)
    def __iter__(self):
        #如果想要一个对象称为一个可以迭代的对象,既可以使用for,那么必须实现__iter__方法
        return ClassIterator()

class ClassIterator(object):
    def __iter__(self):
        pass
    def __next__(self):
        return 11

classmate = Classmate()
classmate.__add__('张三')
classmate.__add__('李四')
classmate.__add__('王五')

#程序判断的流程
print('判断classmate是否是可以迭代的对象:',isinstance(classmate,Iterable))
classmate_iterator = iter(classmate)
print('判断classmate_iterator是否是迭代器:',isinstance(classmate_iterator,Iterator))
print(next(classmate_iterator))  #next(classmate_iterator 表示调用classmate_iterator中的next方法

for name in classmate:
    print(name)

for是否可以使用的判断流程如下:

print('判断classmate是否是可以迭代的对象:',isinstance(classmate,Iterable))
classmate_iterator = iter(classmate)
print('判断classmate_iterator是否是迭代器:',isinstance(classmate_iterator,Iterator))

如果想要一个对象称为一个可以迭代的对象,既可以使用for,那么必须实现__iter__方法。
首先判断Classmate类中是否有__iter__方法,如果有,那么classmate就是可以迭代的对象,接下来在__iter__方法中实例化ClassIterator(在__iter__方法中return ClassIterator())如果ClassIterator类中有__next__方法,那ClassIterator就是是迭代器。至此判断完成。
运行结果:
在这里插入图片描述
完善迭代器

# -*- coding:UTF-8 -*-
from collections import Iterable,Iterator

class Classmate(object):
    def __init__(self):
        self.names = list()
    def __add__(self, name):
        self.names.append(name)
    def __iter__(self):
        #如果想要一个对象称为一个可以迭代的对象,既可以使用for,那么必须实现__iter__方法
        return ClassIterator(self)

class ClassIterator(object):
    def __init__(self,obj):
        self.obj = obj
        self.current_num = 0
    def __iter__(self):
        pass
    def __next__(self):
        if self.current_num < len(self.obj.names):
            ret = self.obj.names[self.current_num]
            self.current_num += 1
            return ret
        else:
            raise StopIteration
classmate = Classmate()
classmate.__add__('张三')
classmate.__add__('李四')
classmate.__add__('王五')

#程序判断的流程
#print('判断classmate是否是可以迭代的对象:',isinstance(classmate,Iterable))
#classmate_iterator = iter(classmate)
#print('判断classmate_iterator是否是迭代器:',isinstance(classmate_iterator,Iterator))
#print(next(classmate_iterator))  #next(classmate_iterator 表示调用classmate_iterator中的next方法

for name in classmate:
    print(name)

运行结果:
在这里插入图片描述
优化迭代器:

# -*- coding:UTF-8 -*-
from collections import Iterable,Iterator

class Classmate(object):
    def __init__(self):
        self.names = list()
        self.current_num = 0
    def __add__(self, name):
        self.names.append(name)
    def __iter__(self):
        #如果想要一个对象称为一个可以迭代的对象,既可以使用for,那么必须实现__iter__方法
        #return ClassIterator(self)
        return self #返回自己
    def __next__(self):
        if self.current_num < len(self.names):
            ret = self.names[self.current_num]
            self.current_num += 1
            return ret
        else:
            raise StopIteration

'''
class ClassIterator(object):
    def __init__(self,obj):
        self.obj = obj
        self.current_num = 0
    def __iter__(self):
        pass
    def __next__(self):
        if self.current_num < len(self.obj.names):
            ret = self.obj.names[self.current_num]
            self.current_num += 1
            return ret
        else:
            raise StopIteration
'''
classmate = Classmate()
classmate.__add__('张三')
classmate.__add__('李四')
classmate.__add__('王五')

#程序判断的流程
#print('判断classmate是否是可以迭代的对象:',isinstance(classmate,Iterable))
#classmate_iterator = iter(classmate)
#print('判断classmate_iterator是否是迭代器:',isinstance(classmate_iterator,Iterator))
#print(next(classmate_iterator))  #next(classmate_iterator 表示调用classmate_iterator中的next方法

for name in classmate:
    print(name)

运行结果
在这里插入图片描述

posted @ 2022-04-24 15:33  南瓜头pumpkin  阅读(10)  评论(0)    收藏  举报