Monday, November 18, 2024
Google search engine
HomeLanguagesPython – Conventions and PEP8

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




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))


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




# 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))


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:

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments