How to tell the difference between an iterator and an iterable?

前端 未结 4 584
醉梦人生
醉梦人生 2021-02-01 08:13

In Python the interface of an iterable is a subset of the iterator interface. This has the advantage that in many cases they can be treated in the same way. However, there is an

4条回答
  •  一向
    一向 (楼主)
    2021-02-01 08:47

    import itertools
    
    def process(iterable):
        work_iter, backup_iter= itertools.tee(iterable)
    
        for item in work_iter:
            # bla bla
            if need_to_startover():
                for another_item in backup_iter:
    

    That damn time machine that Raymond borrowed from Guido…

提交回复
热议问题