Iterators, generators and decorator closures

Iterator

The iterator can be broken into a container. When looping, one data is taken out of the container each time until the data is taken out.

Iterator definition

Need to implement two methods in the class __iter__ and __next__ the
iter method needs to return itself, it is the requirement of the for loop to use the iterator. The
next method is used to return the next element in the container. When the data in the container is exhausted , it needs to be triggered StopIteration


"""
自定义迭代器,通过传入最小值最大值,返回该范围所有数值的3次方
将返回的值,存入num_list列表中
"""
class Nunber():
    num_list = []
    def __init__(self

Guess you like

Origin blog.csdn.net/Mwyldnje2003/article/details/113415741