[Python-ideas] Making colons optional?

Riobard Zhan yaogzhan at gmail.com
Thu Feb 5 20:57:05 CET 2009

On 5-Feb-09, at 3:37 PM, Chris Rebert wrote:

> 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.

Do you notice immediately that the left ( is unbalanced in your  
original example? :P I see it odd right away, too.

Line continuations are not good examples to demonstrate the necessity  
of colons, because soon you will run into the problem of requiring  
semicolons  at the end of every statement. I think Denis made it quite  
clear in a previous reply.

do(some(), some_other(), some_more(complex=(True,)),
         and_final_call(egg=(1,2,3,zzzzz=False)));    # endof  
statement is obvious (if semicolons are required)

> 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).

I think in my original proposal I stated it very clear the colons are  
OPTIONAL and can be used for one-liners, just like semicolons. I did  
not propose to eliminate colons all together. 

More information about the Python-ideas mailing list