In [54]: def TopLevel(a): ...: lastLimit = 3 # kimit on length of lastvalues ...: lastvalues = [0 for i in range(0,lastLimit)] ...: def Closure(b,reset=False): ...: if reset: ...: for index in range(0,len(lastvalues)): ...: lastvalues[index] = 0 ...: # reconstructing lastvalues would require a nonlocal ...: # declaration because it would build a different list ...: print('DEBUG Closure lastvalues', type(lastvalues), lastvalues) ...: some = a + b + sum(lastvalues) ...: lastvalues.pop(0) # returns and discards element at the front ...: lastvalues.append(b) # append onto the back ...: return some ...: return Closure In [54]: def TopLevel(a): ...: lastLimit = 3 # kimit on length of lastvalues ...: lastvalues = [0 for i in range(0,lastLimit)] ...: def Closure(b,reset=False): ...: nonlocal lastvalues ...: if reset: ...: lastvalues = [0 for i in range(0,lastLimit)] ...: print('DEBUG Closure lastvalues', type(lastvalues), lastvalues) ...: some = a + b + sum(lastvalues) ...: lastvalues.pop(0) # returns and discards element at the front ...: lastvalues.append(b) # append onto the back ...: return some ...: return Closure In [239]: def TopLevel(a): ...: lastLimit = 3 # kimit on length of lastvalues ...: lastvalues = [0 for i in range(0,lastLimit)] ...: def Closure(b,reset=False): ...: if reset: ...: for index in range(0,len(lastvalues)): ...: lastvalues[index] = 0 ...: # reconstructing lastvalues would require a nonlocal ...: # declaration because it would build a different list ...: print('DEBUG Closure lastvalues', type(lastvalues), lastvalues) ...: some = a + b + sum(lastvalues) ...: lastvalues.pop(0) # returns and discards element at the front ...: lastvalues.append(b) # append onto the back ...: return some ...: def grow(newLimit): ...: nonlocal lastLimit ...: if newLimit < 1: ...: raise ValueError('INVALID newLimit ' + str(newLimit)) ...: while len(lastvalues) > newLimit: ...: lastvalues.pop(0) ...: while len(lastvalues) < newLimit: ...: lastvalues.append(0) ...: lastLimit = newLimit ...: return (Closure, grow) In [321]: def TopLevel(a): ...: lastLimit = 3 # kimit on length of lastvalues ...: lastvalues = [0 for i in range(0,lastLimit)] ...: def Closure(b,reset=False,grower=None): ...: nonlocal lastLimit ...: if grower != None and grower != lastLimit: ...: if grower < 0: ...: raise ValueError('INVALID newLimit ' + str(grower)) ...: while len(lastvalues) > grower: ...: lastvalues.pop(0) ...: while len(lastvalues) < grower: ...: lastvalues.append(0) ...: lastLimit = grower ...: if reset: ...: for index in range(0,len(lastvalues)): ...: lastvalues[index] = 0 ...: # reconstructing lastvalues would require a nonlocal ...: # declaration because it would build a different list ...: print('DEBUG Closure lastvalues', type(lastvalues), lastvalues) ...: some = a + b + sum(lastvalues) ...: lastvalues.append(b) # append onto the back ...: lastvalues.pop(0) # returns and discards element at the front ...: print('DEBUG lastLimit grower ' + str(lastLimit) + ' ' + str(grower)) ...: return some ...: return Closure In [322]: def genA(a): ...: for value in range(0,a): ...: yield(value) ...: In [323]: ga = genA(10) In [324]: type(ga) Out[324]: generator In [325]: ga Out[325]: In [326]: for value in ga: ...: print(value) ...: 0 1 2 3 4 5 6 7 8 9 In [327]: list(genA(10)) Out[327]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] In [328]: ga = genA(10) In [329]: ga Out[329]: In [330]: ga.__next__() Out[330]: 0 In [331]: ga.__next__() Out[331]: 1 In [332]: ga.__next__() Out[332]: 2 In [333]: ga.__next__() Out[333]: 3 In [334]: ga.__next__() Out[334]: 4 In [335]: ga.__next__() Out[335]: 5 In [336]: ga.__next__() Out[336]: 6 In [337]: ga.__next__() Out[337]: 7 In [338]: ga.__next__() Out[338]: 8 In [339]: ga.__next__() Out[339]: 9 In [340]: ga.__next__() --------------------------------------------------------------------------- StopIteration Traceback (most recent call last) Cell In[340], line 1 ----> 1 ga.__next__() StopIteration: In [341]: ga = genA(10) In [342]: for value in ga: ...: print(value) ...: 0 1 2 3 4 5 6 7 8 9 In [344]: def genA(a): ...: for value in range(0,a): ...: yield(value) ...: if value > 7: ...: return ...: