Continuing indentation

Erik python at lucidity.plus.com
Thu Mar 3 20:06:33 EST 2016


On 04/03/16 00:13, Steven D'Aprano wrote:
> class C:
>      def method(self):
>          if (result is None
>                  or self.some_condition()
>                  or len(some_sequence) > 100
>                  or some_other_condition
>                  or page_count < 5
>              ):
>              do_processing()
>
> Looks fine to me.

Indeed. I don't understand why, when splitting a condition such as this, 
people tend to put the operator at the end of each line.

In C, I also prefer (a style copied from an old colleague of mine who 
had lots of strange ideas, but I liked this one ;)) -

if (  long_condition
   &&  other_long_condition
   &&  (another_long_condition
     || yet_another_long_condition)
   || some_other_condition) {
     process();
}

I just find that so much easier to grok than:

if (long_condition &&
     other_long_condition &&
     (another_long_condition ||
      yet_another_long_condition) ||
     some_other_condition) {
     process();
}

Also, it sort of lays out just what the short-circuit evaluation is 
going to do, so when those long conditions are /actually/ long and 
require a bit of mental parsing, you can scan the left hand side of the 
code and not have to read most of it as you work out which conditions 
may be true. With the second form, you have to parse every line to work 
out if the operator at the end is a top-level operator or part of a 
sub-condition.

E.



More information about the Python-list mailing list