switch recipe?
Cliff Wells
LogiplexSoftware1 at earthlink.net
Fri Jul 12 16:23:50 EDT 2002
On Fri, 2002-07-12 at 13:05, Mark McEahern wrote:
> > out of curiosity, what happens when n reaches the end of the
> > int/long/whatever that stores it?
>
> My guess is this will run forever--if you let it. <wink>
>
> Perhaps it should allow an optional sentinel? The assumption is the caller
> is responsible for using it wisely.
Or on your "generic" version (still untested - I just reinstalled my PC
and don't have Python 2.2 yet):
from __future__ import generators
def make_switch(*args):
"""Return a generator that loops through args."""
if not args:
raise RuntimeError("Missing parameter: args.")
def switch():
i = n = 0
while True:
i = n % len(args)
yield args[i]
n = min(n + 1, len(args))
return switch
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
More information about the Python-list
mailing list