1 回答

TA貢獻(xiàn)1795條經(jīng)驗(yàn) 獲得超7個(gè)贊
您可以使用工廠函數(shù)。例如
import string
def make_inner_reducer_function(arr, required):
? ? red = ['0']
? ? alpha = string.printable[10:62]
? ? ss = ''
? ? it = len(arr) - required + 1
? ? for i in range(required):
? ? ? ? now = alpha[i]
? ? ? ? rd = '-'.join(red)
? ? ? ? ss += '\t' * (i + 1) + f'for {now} in range({it}-{rd}):\n'
? ? ? ? red.append(now)
? ? exec('def inner_reducer():\n' + ss + '\t' * (required + 1) + f'yield {red[-1]}')
? ? return locals()['inner_reducer']
f = make_inner_reducer_function([0, 1, 2, 3, 4], 4)
a = f()
print(a.__next__())
print(a.__next__())
print(a.__next__())
print(a.__next__())
添加回答
舉報(bào)