How does 'yield' work in this permutation generator?
问题 def perm_generator(lst): if len(lst) == 1: yield lst else: for i in range(len(lst)): for perm in perm_generator(lst[:i] + lst[i+1:]): yield [lst[i]] + perm This code has been bugging me, since I don't understand how the yield s connect to each other. My understanding was that yield acts like a return , but it stops temporarily until it is called again. How do these yield s work? 回答1: It might help seeing a version that doesn't use generators: def perm_generator(lst): res = [] if len(lst) == 1