Pharmaceutical company researchers summarized Python coding standards

Introduction

This section describes the Python coding conventions.

Coding rules defined in PEP8

Python has a coding standard called PEP8. The outline specified in PEP8 is shown below.

import statement

In the import statement, do not use ʻimport os, sys`, etc., but divide it into multiple lines as shown below. Also, insert a blank line to distinguish between standard libraries, third-party libraries, local modules, etc.

import os
import sys

from django.utils import timezone

from my_app.models import User

Whitespace characters in the text

a = 1
b = a + 2
list_nums = [a, b]
dict_nums = {'a': a, 'b': b}

Insert a half-width space before and after =, and put a half-width space after , in lists and dictionaries. Also, put a space after the : in the dictionary.

Indent

When defining a ʻif statement, a forstatement, a function or a class, start writing the line after the:` by indenting four half-width spaces (and multiples thereof).

if True:
    print("It's true.")

Blank line

Leave two lines before top-level function or class definitions. Leave one line free for methods in the class.

def my_func():
    return 'my_func'


class MyClass():
    name = my_class

    def print_name(self):
        return self.name

Number of characters per line

The number of characters in one line is basically 79 characters or less. For docstring, use 72 characters or less.

Check using Flake8

There is flake8 as a tool to check if the source code complies with PEP8. It can be executed with the following command.

$flake8 filename.py

When executed, it will show you which parts of the source code do not meet what coding conventions.

Summary

Here, I explained the coding standard of Python. Python is a readability language, so it's essential for writing code that is easy for others to read.

Recommended Posts

Pharmaceutical company researchers summarized Python coding standards
Pharmaceutical company researchers summarized Python control statements
Pharmaceutical company researchers summarized Python unit tests
Pharmaceutical company researchers summarized classes in Python
Pharmaceutical company researchers summarized Python exception handling
Pharmaceutical company researchers summarized variables in Python
Pharmaceutical company researchers summarized SciPy
Pharmaceutical company researchers summarized regular expressions in Python
Pharmaceutical company researchers summarized RDKit
Pharmaceutical company researchers summarized web scraping using Python
Pharmaceutical company researchers summarized Pandas
Pharmaceutical company researchers summarized file scanning in Python
Pharmaceutical company researchers summarized database operations using Python
Pharmaceutical company researchers summarized NumPy
Pharmaceutical company researchers summarized Matplotlib
Pharmaceutical company researchers summarized Seaborn
Pharmaceutical company researchers summarized Python's comprehensions
Pharmaceutical company researchers have summarized the operators used in Python
Pharmaceutical company researchers summarized Python's data structures
How to install Python for pharmaceutical company researchers
First Python ~ Coding 2 ~
Summary of Python articles by pharmaceutical company researcher Yukiya