How do I follow python PEP8 regarding line breaks, and how important is it?
Asked Answered
S

1

8

I am writing in python 3.5.1 and I am a programming novice.

I use gedit with a pep8 and pyflakes plugins showing style mistakes according to the python style guide. I do not know whether to follow the style recommendation to the letter or not.

I have, however, three recurring style flaws indicated by E501: line too long (80 > 79 characters), E502: the backslash is redundant between brackets and E128/E127/...: continuation line under-indented for visual indent. Screenshots below.

My two questions are:

  1. How can I adequately make a line break in python in order to avoid E501 and the subsequent style errors E502 and E127/128?
  2. Is it helpful to follow the style guide pep8 to the letter, especially with publication in mind? What do advanced python programmers say?

E501: enter image description here

E502: enter image description here

E127/128...: enter image description here

Stipple answered 19/1, 2016 at 11:7 Comment(4)
"With publication in mind", yes, absolutely, follow the style guide. No exceptions; standards exist for mutual comprehensibility. If you don't like the style guide, submit a new PEP; in this case, expect it to be rejected.Cementation
Why has this been closed? There's a perfectly good answer to written about point 2, how to do line breaks that will be acceptable to PEP8. Edit - as @Cementation has now provided.Standin
My 2c worth: 1. Definitely don't use backslash continuation when you don't need it, eg inside brackets. 2. Your indentation on those continued lines is way too big. 3. Plenty of people treat the line length thing as a suggestion rather than as a rigid restriction, since modern monitors are a lot bigger than they used to be. OTOH, sticking to the standard will make your code look better in many places, including here on SO.Coelacanth
@msw: I suspect that _() is an i18n function. See https://mcmap.net/q/294368/-meaning-of-leading-underscore-in-list-of-tuples-used-to-define-choice-fieldsCoelacanth
C
13

"How can I adequately make a line break in python in order to avoid E501 and the subsequent style errors E502 and E127/128?"

progress = Utils.ProgressMeter('Source strings separated by white '
    'space are automatically concatenated by the '
    'interpreter and parenthesis are the natural syntax '
    'for line continuation. Remember to use trailing '
    'spaces.')

Since error E502 is already inside parentheses, the backslash is redundant. Did you try eliminating it?

Cementation answered 19/1, 2016 at 11:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.