Recursive function with yield doesn't return anything
问题 I am trying to create a generator for permutation purpose. I know there are other ways to do that in Python but this is for something else. Unfortunately, I am not able to yield the values. Can you help? def perm(s,p=0,ii=0): l=len(s) s=list(s) if(l==1): print ''.join(s) elif((l-p)==2): yield ''.join(s) yield ''.join([''.join(s[:-2]),s[-1],s[-2]]) else: for i in range(p,l): tmp=s[p] s[p]=s[i] s[i]=tmp perm(s,p+1,ii) 回答1: Your line perm(s,p+1,ii) doesn't do anything, really: it's just like