Discuss / Python / 换成next一次输出一次

换成next一次输出一次

Topic source

def create_counter():

    i = 0

    def counter():

        nonlocal i

        while True:

            i += 1

            yield i

    return counter

gc = create_counter()()

print([next(gc) for i in range(10)])


  • 1

Reply