python iterators.py

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python iterators.py相关的知识,希望对你有一定的参考价值。

# iterable class

class naturals(object):
    def __init__(self, top):
        assert top >= 1, 'top >= 1'
        
        self._cur = 1
        self._top = top
    
    def __iter__(self):
        return self
    
    def __next__(self):
        if self._cur > self._top:
            raise StopIteration()
        
        tmp = self._cur
        self._cur += 1
        return tmp

# generator function

def naturals(top):
    n = 1
    while n <= top:
        # `yield x` puts the value `x` on hold, and halts function execution.
        # the result is a generator object, which follows the iterator pattern.
        # calling next() on one of these will return `x` and resume function
        # execution, until another `yield` statement is found or the function ends
        yield n
        n += 1

# for-in iteration

for n in naturals(10):
    print(n)

# manual iteration (naive, imho)

it = naturals(10)
try:
    while True:
        n = next(it)
        print (n)
except StopIteration:
    pass

# manual iteration (generic)

nums = naturals(10)
it = iter(nums) # make sure we are dealing with an iterator object
sentinel = object() # create unique object

while True:
    n = next(it, sentinel) # next returns sentinel instead of raising exception
    if n is sentinel:
        break
    print(n)

以上是关于python iterators.py的主要内容,如果未能解决你的问题,请参考以下文章

001--python全栈--基础知识--python安装

Python代写,Python作业代写,代写Python,代做Python

Python开发

Python,python,python

Python 介绍

Python学习之认识python