question about for cycle

fdu.xiaojf at gmail.com fdu.xiaojf at gmail.com
Sat Sep 29 23:32:01 EDT 2007


George Sakkis wrote:
> On Sep 29, 10:34 am, "fdu.xia... at gmail.com" <fdu.xia... at gmail.com>
> wrote:
>> tokl... at gmail.com wrote:
>>
>>  > On 29 sep, 12:04, "fdu.xia... at gmail.com" <fdu.xia... at gmail.com> wrote:
>>  >
>>  >> for i in generator_a:  # the first "for" cycle
>>  >>      for j in generator_b:
>>  >>          if something_happen:
>>  >>              # do something here ..., I want the outer cycle to break
>>  >>              break
>>  >
>>  > Do you like this?
>>  >
>>  > generator_ab = ((x, y) for x in generator_a for y in generator_b)
>>  > for i, j in generator_ab:
>>  >     if condition:
>>  >         # do something
>>  >         break
>>  >
>>   In this case, the tuple generator_ab must be generated first.
> 
> It's not a tuple, it is a generator expression that can generate
> tuples *lazily*.
> 
>> Sometime it maybe a waste to generate all possible combinations of i,j first.
> 
> It doesn't; read about generator expressions at http://www.python.org/dev/peps/pep-0289/
> 
> George
> 
Thanks, I didn't realize that.

However, I still think labeled break and continue is a valuable feature, which 
is easier to understand and to use.




More information about the Python-list mailing list