Discuss / Python / 终于搞懂了

终于搞懂了

Topic source

def createCounter():

def ge():
    n = 1
    while True:
        yield n
        n += 1

g = ge()
def counter():
    return next(g)

return counter

他国行尸

#2 Created at ... [Delete] [Delete and Lock User]

用迭代器好,那些用nonlocal的跳步了

Why my code doesn't work?

def createCounter(): def counter(): i=0 while True: i=i+1 yield i

def caller():
    return next(counter())
return  caller

They are similar. But I didn't use a variable to accept function counter(). Why I cannot directly return next(counter())?

Is there any difference between:

return next(counter())

AND

c=counter() return next(c)?

I cannot understand this.


  • 1

Reply