On 19 Apr, 2012, at 15:29 , Tino Dai wrote:

> Hi!
> 
>      I have a question about style. In PEP-8, it says don't exceed 79 
> characters, but can this rule ever be trumped by 
> readability?
> 

Yes, it obviously can.
I am a big fan of the 79 character "rule", though. Even with screens and 
terminals being well over 79 characters wide these days: with the 79 character 
limit, I can have multiple things side by side (*without* using multiple 
screens).


> Eg.
> 
>      
>      if someobject.filter(something) \
>           .filter(somethingreallyreallyreallyreallylong == 
> somethingelsereallyreallylong) \
>           .filter(othethingreallylongreallyreally == 
> ternarythingreallyreallyreallylong) \
>           .filter(thirdthingreallylessreallymaybelong == 
> thirdotherthingreallylong) \
>           .first():
>           < do something >
> 
>       if someobject.filter(something) \
>           .filter(somethingreallyreallyreallyreallylong == \
>                somethingelsereallyreallylong) \
>           .filter(othethingreallylongreallyreally == \
>               ternarythingreallyreallyreallylong ) \
>           .filter(thirdthingreallylessreallymaybelong == \
>                thirdotherthingreallylong) \
>           .first():
>           < do something >

I would let the parenthesis do the line breaking, so not use backslashes (a 
hidden space after a backslash can be hard to spot).

In my case, it would become something like:

     if someobject.filter(
         something).filter(
         somethingreallyreallyreallyreallylong ==
         somethingelsereallyreallylong).filter(
         othethingreallylongreallyreally ==
         ternarythingreallyreallyreallylong).filter(
         thirdthingreallylessreallymaybelong ==
         thirdotherthingreallylong).first():
               pass

Of course, this isn't very readable either: the filter() function starts at the 
end of a line, and the comparison is broken over two lines.
Note that I've left my editor (emacs) do the indentation. It would have helped 
to indent the second part of the comparison just a bit.


Then again, one can (and should) ask whether 
somethingreallyreallyreallyreallylong is really a good name for a variable.
Occasionally, it does happen in case of dicts within dicts within list, eg 
mylist[counter]['somekey']['anotherkey'], but those are the cases where you 
would assign this to a separate (temporary) variable.


> The first example is more readable to me but violates the 80 character rule. 
> The second is less readable, but doesn't violate
> the 80 character rule.
> 
> Is there a guideline or convention that pythonistas follow about this style 
> case?

Not as such, I think.
But, if lines get long or indentation gets heavy (4 or more indents), it may be 
good to consider two things:
- move parts to a separate function
- assign parts to a temporary variable.

In your case, the outcome of the various comparisons could be assigned to 
individual variables:

result1 = somethingreallyreallyreallyreallylong == somethingelsereallyreallylong
result2 = othethingreallylongreallyreally == ternarythingreallyreallyreallylong
etc
and then the last line becomes
someobject.filter(something).filter(result1).filter(result2).filter(…

Or, since filter() apparently returns a new object:
results1 = someobject.filter(something)
results2 = result1.filter(somethingreallyreallyreallyreallylong == 
somethingelsereallyreallylong)
results3 = result2.filter(othethingreallylongreallyreally == 
ternarythingreallyreallyreallylong)
etc

In both cases, you have changed the backslashes into actual separate statements.
(And yes, those lines still are longer than 79 characters, but to me it becomes 
more readable overall, even if you would break those individual statements.)


So, basically, when you run into something like your suggestion here, try and 
see another way of breaking it up then just splitting across multiple lines.

Cheers,

  Evert


> 
> Thanks,
> Tino
> 
>             
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor

_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

Reply via email to