Else statement executing when it shouldnt

Frank Millman frank at chagford.com
Fri Jan 25 03:15:56 EST 2013


On 23/01/2013 15:35, Jussi Piitulainen wrote:
> Thomas Boell writes:
>
>> Using a keyword that has a well-understood meaning in just about
>> every other programming language on the planet *and even in
>> English*, redefining it to mean something completely different, and
>> then making the syntax look like the original, well-understood
>> meaning -- that's setting a trap out for users.
>>
>> The feature isn't bad, it's just very, very badly named.
>
> I believe it would read better - much better - if it was "for/then"
> and "while/then" instead of "for/else" and "while/else".
>
> I believe someone didn't want to introduce a new keyword for this,
> hence "else".
>

There is a scenario, which I use from time to time, where 'else' makes 
perfect sense.

You want to loop through an iterable, looking for 'something'. If you 
find it, you want to do something and break. If you do not find it, you 
want to do something else.

for item in iterable:
     if item == 'something':
         do_something()
         break
else:  # item was not found
     do_something_else()

Not arguing for or against, just saying it is difficult to find one word 
which covers all requirements.

Frank Millman





More information about the Python-list mailing list