Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 636
Til Hund Avatar asked Jan 19 '16 11:01

Til Hund


People also ask

What is PEP8 in Python and why is it important?

PEP 8, sometimes spelled PEP8 or PEP-8, is a document that provides guidelines and best practices on how to write Python code. It was written in 2001 by Guido van Rossum, Barry Warsaw, and Nick Coghlan. The primary focus of PEP 8 is to improve the readability and consistency of Python code.

Do I need to follow PEP8?

PEP8 is for humans but your program will run even if you do not follow it. If you do not share your code and do not plan to do so, do whatever you want. If you plan to share some part of your code someday, then you should follow PEP8. I mean, probably nobody will care if a few lines are 85 characters.

How many blank lines does the PEP8 Standard recommend to use between statements in a function definition in Python?

One blank line should be both before and after method definitions. You should use blank lines conservatively within your code to separate groups of functions.

How many blank lines does the PEP 8 specification recommend between methods of a particular class?

Surround top-level function and class definitions with two blank lines. Method definitions inside a class are surrounded by a single blank line. Extra blank lines may be used (sparingly) to separate groups of related functions.


1 Answers

"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?

like image 128
msw Avatar answered Sep 17 '22 13:09

msw