Python Rocks! - get rid of colons

Mark Hathaway hathawa2 at marshall.edu
Thu Jan 20 11:43:21 EST 2000


>> [Sposhua]
>>
>> I'm one of the "prefers indentation to {}" ppl, and I'd
>> just like to add: whe the heck do we need colons (:) at
>> the end of if's 'n loops anyway?  The only loss I see is
>> to these 1-liners:
>>
>> if a==b: eric=idle
>>
>> What's wrong with:
>>
>> if a==b
>>       eric=idle

> Tim Peters wrote:
>
> As Michael Hudson said, the connection between colons and blocks was adopted
> from Python's closest predecessor, ABC.  Formal usability studies showed
> that while indentation was less confusing than assorted bracketing
> constructs to newbies, the combination of indentation and an introductory
> colon was even better.  You're right that the grammar has little use for it;
> although, pragmatically, it's a great aid to semi-stupid tools (like Python
> editor modes -- not to mention all us Python programmers <wink>).
>...
> Ambiguity, and Yet Another Special Meaning for Parens, were taken as bad
> omens.  So Guido exorcised those demons.

Ambiguity is indeed an important thing, and for both humans and the
software
which interprets the code. So, what's with the "elsif:" and "else:"
colon?

I can see leaving out the "end" closure for "if", "while" & "for"
statements
to ensure they can be executed cleanly as one-liners, but is it safe to
consider
the indent as (effectively) a keyword? Isn't it less ambiguous to use a
keyword
to ensure the author's meaning isn't lost. Surely a visible keyword is
less
likely to be destroyed than spaces. The author's intent with spaces
isn't
necessarily obvious, but an "end" is forever.  :-)

if <boolean-test>:
    <statement-list>
elsif
    <statement-list>
else
    <statement-list>
end

There's the keyword "if" at the beginning, the ":" after the boolean
test,
and the other keywords to separate the statement lists. Clean and
perfectly
unambiguous what is meant by the author.

while <boolean-test>:
    <statement-list>
end

for <iteration-stuff>:
    <statement-list>
end

There's no excessive use of the ":" and the unambiguity of using
the closure "end" is clear for all to see.

Also, to counter the argument of someone who gave a wild example of
using
the "end", but not enforcing the indentation rules; enforce the
indentation
rules AND use "end". It's even clearer that way.



More information about the Python-list mailing list