[Python-ideas] Making colons optional?

Lie Ryan lie.1296 at gmail.com
Tue Feb 10 08:04:52 CET 2009


On Thu, 05 Feb 2009 12:07:39 -0500, Scott Dial wrote:

>>     if (some() and some_other() or some_more(complex=(True,))
>>         and a_final_call(egg=(1,2,3))):
>>         do_something()
> 
> This example uses the same mechanism as above. BTW, I tend to indent
> this as:
> 
>     if (some() and some_other() or some_more(complex=(True,))
>             and a_final_call(egg=(1,2,3))):
>         do_something()
> 
> With or without the colon, and it's more readable than your version

<nitpick>

of course a more pythonic approach would format it as this:

if (some() and some_other() or some_more(complex=(True,)) and
         a_final_call(egg=(1,2,3))):
    do_something()

It is clear that the if-clause continues to the next line because there 
is an unsatisfied "and" in the end of it and it is also consistent with 
PEP 8's guideline for having operators/keywords before breaking.

alternatively:

if some() and some_other() or some_more(complex=(True,)) and \
         a_final_call(egg=(1,2,3)):
    do_something()

it is even clearer since there is a line continuation token "\".

personally, I like this:

if some() and some_other() or \
   some_more(complex=(True,)) and \
   a_final_call(egg=(1,2,3)) \
    :
    do_something()

anything more complex than a very simple expression in a multi-line if 
should really be on its own line, irrespective of whether it has reached 
the character limit or not.

Even more preferably though, that expression shouldn't be that complex, 
it becomes hard to follow the logic of the if. The program require a 
little bit of factoring if I saw such expression.
</nitpick>




More information about the Python-ideas mailing list