Python – Conventions and PEP8

Convention means a certain way in which things are done within a community to ensure order. Similarly, Coding conventions are also a set of guidelines, but for a programming language that recommends programming style, practices, and methods. This includes rules for naming conventions, indentations, comments, statements, programming practices, etc.

Following these set of rules ensures the code readability as well as easy maintenance. As code is not always maintained by the developer, therefore the next person must understand your code accurately, as “Code is read much more often than written”.

PEP-8

PEP-8 is an acronym for Python Enhancement Protocol 8, which is a set of guidelines published for the Python programming language. PEP-8 guidelines may seem pedantic, but following them can improve your code, especially when it comes to sharing your code, whether it is your potential employer or open-source contribution or during group projects. And writing clear, readable code is also a sign of professionalism, and shows that you know how to structure your code.

Let’s see why conventions are important in coding using code examples:

Violating PEP-8



filter_none

edit
close

play_arrow

link
brightness_4
code

L = [1, 2, 3, 4, 2, 4, 1, 2]
from collections import defaultdict
# Helper Function
def ltd(l):
    """Convert list to DefaultDict"""
                d = defaultdict(int)
                for i in l:
                         d[i] += 1
                return d
print(ltd(L))

chevron_right


Output:

defaultdict(, {1: 2, 2: 3, 3: 1, 4: 2})

Above is a code that violates PEP-8 best practices, to put it simply the code here is hard to read. Few problems are, the import module statement isn’t at the top of the file, the spaces and indentations are inconsistent and the lack of linebreaks makes it difficult to understand when the function ended and the next lines of code begin. Even without the knowledge of PEP-8 rules whatsoever, you can probably tell this isn’t the most readable chunk of code. Let’s see this code after when it is rewritten with PEP-8 regulations.

Following PEP-8

filter_none

edit
close

play_arrow

link
brightness_4
code

# Import needed package
from collections import defaultdict
  
# Define our data
list_data = [1, 2, 3, 4, 2, 4, 1, 2]
  
  
# Helper Function
def list_to_dict(input_list):
    """Convert list to DefaultDict"""
    d = defaultdict(int)
    for i in input_list:
        d[i] += 1
    return d
  
  
# Output
print(ltd(list_data))

chevron_right


Output:

defaultdict(, {1: 2, 2: 3, 3: 1, 4: 2})

As you can notice above, the same piece of code now depicts the appropriate usage of white space, indentations, and linebreaks which makes the code look much cleaner than before. And now with the help of comments, the code is much more understandable than before.

PEP-8 Tools

If you’re new to Python, it can be a bit difficult to remember what certain piece of code does after a few days or weeks of writing your program. If you follow PEP 8, you can be sure that you’ve named your variables well and added enough whitespace so it’s easier to follow logical steps in your code. You’ll also have commented your code well. All this will mean your code is more readable and easier to understand. As a beginner, following the rules of PEP 8 can be difficult but there are various tools you can use to ensure that your code is following the rules, such as:

  • IDE like PyCharm, which flags violation as soon as you write a bad line of code.
  • Using pycodestyle package, it can check code in multiple files at once, and it outputs descriptions of the violations along with the information of where you need to go to fix the issue.
    Step 1: Install pycodestyle using pip install, type the below command in the terminal.

    pip install pycodestyle

    Step 2: Check the python file with pycodestyle using command as shown below.(pycodestyle **filename**.py)

    Output:

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

6


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.