strange list comprehension on generator

Roland Puntaier Roland.Puntaier at br-automation.com
Wed Aug 29 15:50:15 CEST 2007


def changeOne(aa,idx):
  aa[idx]=not aa[idx]
  yield aa
  for i in range(idx):
    for x in changeOne(aa,i):
      yield x

def changeOneOrder(aa):
  yield aa
  for i in range(len(aa)): 
    for x in changeOne(aa,i):
      yield x

a=[False]*3
og=changeOneOrder(a)
#this does not return the way I would expect. why?
list(og)
#returns
#[[False, False, True], [False, False, True], [False, False, True], 
[False, False, True], [False, False, True], [False, False, True], [False, 
False, True], [False, False, True]]


#this works as expected
a=[False]*3
og=changeOneOrder(a)
og.next()
og.next()
...

#this works as expected
def ty():
  yield 1
  yield 2
  yield 3

tg=ty()
list(tg)

cheers, Roland




More information about the Python-list mailing list