[Tutor] Correct style of line break when chaining methods

Mats Wichmann mats at wichmann.us
Sat Jul 10 11:49:59 EDT 2021


On 7/10/21 9:38 AM, Manprit Singh wrote:
> Dear sir,
> 
> consider a line of code :
> 
> csknum = crick.iloc[:, [3, 4, 5]].isin(["CSK"]).all(axis="columns").sum()
> 
> This line contains chained methods . What would be an appropriate way to
> break this line ?
> The way i have done it is given below :
> 
> csknum = (crick.iloc[:, [3, 4, 5]]
>                    .isin(["CSK"])
>                    .all(axis="columns")
>                    .sum())
> 
> Need your comments

1. that line doesn't need breaking, it's short enough
2. try not to - it might be a place where you're okay to go a bit over 
80 columns
3. if necessary, the above is okay, but if you're going to use parens as 
a way to be able to break the line, you might break right after the 
opening paren so you're not tryint to artificially line up chunks 
underneath something that's a ways off to the right.  By which I mean:

csknum = (
     crick.iloc[:, [3, 4, 5]]
     .isin(["CSK"])
     .all(axis="columns")
     .sum()
)


these are but opinions!


More information about the Tutor mailing list