2

I have a long line that pylint is complaining is breaking pep8 E501

count_approvers = self.leave_not_required_user.company.leave_approvers.count()

How can I split this over 2 lines?

tread
  • 10,133
  • 17
  • 95
  • 170
  • https://stackoverflow.com/questions/4172448/is-it-possible-to-break-a-long-line-to-multiple-lines-in-python – sshashank124 Feb 20 '18 at 09:14
  • 1
    I mean, you can just break the line with `\\`... it's a trivial question ;) – cs95 Feb 20 '18 at 09:14
  • I don't think this is a particularly long one-liner, although it might be longer with indentation. – tobias_k Feb 20 '18 at 09:15
  • Aha, thank you. I didn't know you could do this. If you look at the "duplicate" question there are no examples with a pure code line. They all have strings. – tread Feb 20 '18 at 09:17
  • 1
    Also, instead of splitting the one-liner over multiple lines, IMHO making it less readable, consider making it _not_ a one-liner, e.g. defining `comp = self.leave_not_required_user.company` first. – tobias_k Feb 20 '18 at 09:18
  • read this Python pep8 coding guidelines. https://www.python.org/dev/peps/pep-0008/ – Binit Amin Feb 20 '18 at 09:18

2 Answers2

3

Configure your pylint for length of line. Or you can use '\' after '='

count_approvers = \
        self.leave_not_required_user.company.leave_approvers.count()
Binit Amin
  • 481
  • 2
  • 18
0

You could use a temporary alias, if you may:

company = self.leave_not_required_user.company 
count_approvers = company.leave_approvers.count()
Daniel
  • 11,332
  • 9
  • 44
  • 72