.feed-links {display:none !important;} -->

PEP 8 learn best practices for coding in python

 

What is PEP 8?

PEP 8 (for Python Extension Proposal) is a set of rules that allows code to be homogenized and best practices to be applied. The best known example is the war between developers over whether to indent code with spaces or with tabs. PEP 8 slices: the spaces win, 4 in number. End of the debate.

It is a very interesting logic for Python which wants to be a language "which is read" rather than "a language which is written". So yes this sentence does not mean anything but writing an understandable code for a machine is rather easy, but writing a code understandable by the greatest number of coders (of any level) is another story ...

PEP 8 makes it possible to establish rules and conventions to make it easier for the coder to read and thus make him less stressed and more productive. The advantage of PEP 8 makes it possible to embellish the code. So I still don't know if the code is embellished with PEP 8 or if making rules doesn't make the code without PEP 8 ugly. We can tell ourselves when reading a script that the author has not respected these basic rules and therefore is not a good coder. This is not necessarily the case but ...

More rules to learn?

It is not necessary to learn all the rules of PEP 8, I will list the most interesting rules here.

Note that a good IDE will help you write your code. It will highlight the inconsistencies and even offer to correct them. Tools like Black will help you get in shape, for example.

Encoding

For python 3: UTF-8 , yes the base. Is it still necessary to specify it? Never use anything other than utf-8.

Indentation

The indentation of your code must be 4 characters long. More is too much, less is not enough. On the side of coffeescript for example it's 2, it's ugly. Shame on them. It had to be written somewhere, here it is.

Layout code

79 characters per line, no more.

Writing in python is like writing some kind of Alexandrian poem. There is the substance but also the form. If you are a perfectionist, review your code, even for a single overflow character; it will not change the execution of your code, it will only waste your time,
but passion is passion, rules are rules.

Import

The imports are declared at the beginning of the script. It seems obvious, but it's always good to remember it. You can add it in a function as well (if you cannot do otherwise or for exceptional reasons) but after the docstring. Provide one line per import .

The spaces

So the spaces you have to be concise, avoid putting them there you don't have to:

name  =  'Batman'  # yes it's clean 
name = 'name'  # no it's ugly 
batmobile  [ 'color' ]  =  'black'  # no 
batmobile [ 'color' ]  =  'black'  # yes

Comment your code in English

We recommend that you comment on your code in English, so that you can share it with as many people as possible.

But if you are in a French-French team or if you are the only one working on your project, it is better to write good comments in French than bad in English.

No comments:

Post a Comment