switch recipe?

Alex Martelli aleax at aleax.it
Fri Jul 12 17:51:58 EDT 2002


Mark McEahern wrote:

>> or possibly:
>> 
>>  def make_switch(*args):
>>       """Return a generator that loops through args."""
>>       if not args:
>>           raise RuntimeError("Missing parameter: args.")
>>       def switch():
>>           while True:
>>               for a in args:
>>                  yield a
>>       return switch
> 
> Holy cow, that's much nicer!

Wouldn't you get exactly the same observable effect from
the simpler function:

 def make_switch(*args):
      """Return an iterator that loops through args."""
      if not args:
          raise RuntimeError("Missing parameter: args.")
      return iter(args)

...?


Alex




More information about the Python-list mailing list