随机序列迭代器randSeq.py&&任意项的迭代器anyIter.py

randSeq.py

from random import choice

class RandSeq(object):
    def __init__(self,seq):
        self.data=seq


    def __iter__(self):
        return self

    def  next(self):
        return choice(self.cata)
    

anyIter.py

classs AnyIter(object):
    def __init__(self,data,save=False):
        self.safe=safe
        self.iter=iter(data)

    def __iter__(self):
        return self


    def next(self,howmany=1):
        retval=[]
        for eachItem in range(howmany):
            try:
                retval.append(self.iter.next())
            except StopIteration:
                if self.safe:
                    break
                else:
                    raise
        return retval



你可能感兴趣的:(随机序列迭代器randSeq.py&&任意项的迭代器anyIter.py)