[Python-ideas] Making colons optional?

Chris Rebert pyideas at rebertia.com
Thu Feb 5 20:07:27 CET 2009

On Thu, Feb 5, 2009 at 8:05 AM, Riobard Zhan <yaogzhan at gmail.com> wrote:
> On 5-Feb-09, at 12:37 AM, Ben Finney wrote:
>>> - We already have indentation to visually separate different levels
>>> of code. Why bother with those extra colons at all?
>> Because indentation also occurs for other reasons, a major example
>> being continuations of previous lines.
>>   for line in foo:
>>       do_something(
>>           spam, line)
>> That's three lines at differing indentation levels, but two
>> statements.
>> I find that the line-end colon is a strong visual indicator that a
>> suite is being introduced, as contrasted with some other difference in
>> indentation.
> Actually this is the first concrete example for colons. Thanks very much for
> bringing it up, Ben! :)
> Here is a counter-example for the strong visual indicator cause.
>    for some_list in some_collection:
>        do_something(some_list[1:
> ], something_else)

True; however, the parentheses and brackets are unbalanced and that
immediately stands out, at least for me, so I see something odd is
going on right away.
You do have to admit that example is a bit contrived though. Move the
`],` back onto the previous line and the code becomes perfectly clear.

I would also defend colons on the grounds that they let particularly
short statements be one-liners:

def add(x,y): return x+y

#this one especially comes up a lot in practice
if x is None: x=42

for i in alist: print(i)

with the added bonus that you're forced to indent if the body becomes
multiline (unless you use semicolons that is, in which case you're
destined to burn in the Nth circle of Hell for your sin against the
BDFL (blessed be his holiness) ;-P).

-1; colons enhance readability and are almost never forgotten after
you get over the initial hurdle of learning a new language.


Follow the path of the Iguana...

More information about the Python-ideas mailing list