Understanding while...else...
Oscar Benjamin
oscar.j.benjamin at gmail.com
Wed Jan 23 06:18:31 EST 2013
On 23 January 2013 11:03, René Klačan <rene.klacan at gmail.com> wrote:
> On Wed, Jan 23, 2013 at 1:39 AM, Oscar Benjamin <oscar.j.benjamin at gmail.com> wrote:
You missed off an important piece of context in your post:
>> I think he meant that he would use the else clause more often if it
>> had the semantics so that the two blocks below were equivalent:
The key word in that sentence is "if".
>> # Version 1
>> while condition:
>> # stuff
>> else:
>> # other stuff
>>
>> # Version 2
>> if condition:
>> while condition:
>> # stuff
>> else:
>> # other stuff
>
> they wouldnt be equivalent if #staff in version did not cointain "break"
> statement and this is common mistake
I realise that they are not equivalent. My point was that some people
expect, or would prefer, different behaviour so that those two *would*
be equivalent (assuming that evaluating "condition" doesn't have side
effects).
Oscar
More information about the Python-list
mailing list