> So here's a different approach, which I think does meet the spec:
>
> from itertools import tee
> def allwords2(alphabet="abcd", maxlen = 4):
>      def wordgen():
>          for char in alphabet:
>              yield char
>          for partial in allwordstee[1]:
>              if len(partial) == maxlen:
>                  raise StopIteration
>              for char in alphabet:
>                  yield partial+char
>      #tee creates two copies of the iterator:
>      # one is returned to the caller
>      # the other is fed back to the generator
>      allwordstee = tee(wordgen())
>      return allwordstee[0]

Very neat. I never knew about tee.
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to