[Tutor] Correct style of line break when chaining methods

Manprit Singh manpritsinghece at gmail.com
Sat Jul 10 12:03:06 EDT 2021


Dear sir,

See in this particular example, the length of the line is 75 characters ,
and according to PEP 8, in Python a line must not exceed 69 characters.
So I divided this line .
And Secondarily when doing work with Numpy arrays and Pandas we are mostly
working with chained methods, and at that time, what i feel is a line break
can increase readability also .
Need further comments and guidance.

Regards
Manprit Singh

Regards


On Sat, Jul 10, 2021 at 9:20 PM Mats Wichmann <mats at wichmann.us> wrote:

> 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!
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
>


More information about the Tutor mailing list