Python 2.4 | 7.3 The for statement
Bengt Richter
bokr at oz.net
Fri Mar 25 19:42:49 EST 2005
On 25 Mar 2005 15:41:25 -0800, "brainsucker" <jrodrigog at gmail.com> wrote:
>Franciso, some more code.
>
>Breaking with two conditions, and fun with exceptions:
>
>moflo = 1
>try:
> for item1 in range(10):
> if (item1 * moflo) == 3: raise StopIteration
> for item2 in range(10):
> if (item2 * moflo) == 2: raise StopIteration
> print "Let's see"
>except StopIteration:
> pass
>
>As oppossed to:
>
>leave = False
>moflo = 1
>for item1 in range(10) until ((item1 * moflo) == 3) or leave:
> for item2 in range(10) until leave:
> if (item2 * moflo) == 2: leave = True
> print "Let's see"
>
>What can I say.
>
You could play with until in this form (in 2.4 generator expressions):
>>> def until(cond):
... if not cond: return True
... raise StopIteration
...
>>> for x in (x for x in xrange(10) if until(x==3)): print x,
...
0 1 2
actually, that until would read better as "notyet" after the "if"
Regards,
Bengt Richter
More information about the Python-list
mailing list