Pep-8 is the Style Guide for Python Code. As many of you know, PEP is "Python Enhancement Proposals". They provide standard reference points to write code in pythonic way. PEP-8 is the style guide for writing python code. It aims at providing recommendations and standardise the formatting, naming conventions, proper commenting and more. The zen of python says "Readability counts" and it evolved with the fact that "Code is read more often than it is written". If the code is not readable, it's not understood. If it's not understood properly, it can't be maintained. If it can't be maintained, it will be eventually discarded. The PEP-8 helps you in writing maintainable, long living code. PEP-8 is available at he official python site here
Why is it so important ?
The PEP-8 itself says "A style guide is about consistency. Consistency with this style guide is important. Consistency within a project is more important. Consistency within one module or function is the most important." But know when you can be inconsistent. Ignore some guides. One thing I do not stick to, but set my own standard is with "Limit all lines to a maximum of 79 characters". We set the character limit as 150 as we use wider screens and we can avoid lot of extra typing due to descriptive and long names. I recommend "background_color" over "bg_clr" and "file_name_template" over "fnt". One line i wrote today is
file_name = filename_template.format(root_directory, parent_directory, name, extension) This line alone is 88 characters. You may write it as
file_name = filename_template.format( root_directory, parent_directory, name, extension )
I always felt former is more readable. Let me be clear, neiter is wrong, and how you look at it depends on you. I am pretty sure former is not complicated.
Ignoring PEP-8 is like shooting yourself at the foot. Better stick to it and write maintainable code.
How will I remember all these standards?
In reality you don't have to by heart all. Most code editors has plugins and command-line tools that can help you with PEP-8 recommendations. There are two types of tools. First one auto correct the code and second one shows recommendations. Former has the probability of making errors for some cases and you may wonder what went wrong. Using later gives suggestions and fixing manually will make you learn PEP-8 and you will end up writing PEP-8 code eventually.
Search for posts
04 Jan, 2018
01 Jan, 2018
06 Jan, 2018
03 Jan, 2018
13 Oct, 2020
12 Oct, 2020
10 Oct, 2020