[Python] How to write a docstring that conforms to PEP8

If you don't follow the PEP8 rules, your dog will bark !?

In python, there is a style guide and coding standard called PEP8. In the case of a team, by checking the code to see if it meets the PEP8 convention, it will be aligned in a common writing style, making it more readable and easier to develop. You can check PEP8 when you push to github etc. or when you make a pull request. At that stage, CI tools are used to test and notify you of any points that violate the rules. One such tool is reviewdog. ファイル名

If you act against the PEP8 rules, you will be barked by this dog. .. ファイル名

I was told that the dog kept barking at the errors H404 and H405 related to the docstring, and I tried my best to fix the code and let the dog forgive me.

Contents of error code H4xx

H4xx series error codes are related to ** docstring **. A docstring is a place to describe a function or class method when you create it. By the way, this looks good docstring, which makes dogs bark! !! (Not fully compliant with PEP8)

def docstring_sample(hoge1: str):
    """ 
This is the docstring.

If there is an argument, it will be explained here. (There are some templates, so check them out.)
This time in numpy style

    Parameters
    ----------
        hoge1 : str
Output the character string entered here"""
    print(hoge1)

There are other error codes for many things [here](https://blog.sideci.com/about-style-guide-of-python-and-linter-tool-pep8-pyflakes-flake8-haking-pyling -7fdbe163079d) is nicely organized!

Let's take a look at the contents of 4xx!

I'm not good at English, so I had a hard time understanding H404 and H405 ... I will translate it (it is a free translation)

-[H401] Do not start the description of Docstrings with a space. -[H403] When writing a multi-line docstring, start a new line on the last line and then close it. -[H404] When writing a multi-line docstring, leave one line first and then write. -[H405] When writing a multi-line docstring, write a summary of the function on one line before you start writing.

about it.

When I read this, I "I see, that means that in the previous sample, there was no line break at the end, and there was no space at the beginning, so let's rewrite it." Does that mean something like this?

def docstring_sample(hoge1: str):
    """ 

A function that outputs the argument hoge1. (Because I was told to write a summary at first)
This is the docstring.

If there is an argument, it will be explained here. (There are some templates, so check them out.)
This time in numpy style

    Parameters
    ----------
        hoge1 : str
Output the character string entered here
    """
    print(hoge1)

However, this makes the dog bark **. The error code is H405. Why. .. So I melted it for a few hours. (For me, it's a rule that you can't merge into master as long as the dog barks ...)

approach

After a few hours of doing various things, the dog finally recognized me! !! that is

Write H405 at the very beginning of the docstring!

In other words, write a summary before breaking a line-> break a line.

Finally, we have a docstring that complies with the PEP8 convention!

def docstring_sample(hoge1: str):
    """A function that outputs the argument hoge1.
    
This is the docstring.
If there is an argument, it will be explained here. (There are some templates, so check them out.)
This time in numpy style

    Parameters
    ----------
        hoge1 : str
Output the character string entered here
    """
    print(hoge1)

By the way, if you write only one line of docstring, you don't have to break the " "" at the end of the sentence.

def docstring_sample(hoge1:str):
    """Function that outputs the argument hoge1"""
    print(hoge1)

Summary

In PEP8, you can ignore this as expected, right? I think there are several. (Limitation on the number of characters in one line) However, if you control yourself and write it in a lawful manner, it will give you a sense of accomplishment! Please try it!

Below are some references! I also refer to the numpy style and always use it! Thank you for being so easy to understand!

References

https://blog.sideci.com/about-style-guide-of-python-and-linter-tool-pep8-pyflakes-flake8-haking-pyling-7fdbe163079d

[Python] Learn how to write a docstring to improve readability (NumPy style)

Recommended Posts

[Python] How to write a docstring that conforms to PEP8
How to write a Python class
How to write a metaclass that supports both python2 and python3
How to write a list / dictionary type of Python3
How to write a test for processing that uses BigQuery
Qiita (1) How to write a code name
[Python] How to make a class iterable
[Python] How to convert a 2D list to a 1D list
Write the test in a python docstring
[Python] How to invert a character string
How to get a stacktrace in python
How to write a docstring to create a named tuple document with sphinx
I made a library that adds docstring to a Python stub file.
How to write Ruby to_s in Python
How to run a Maya Python script
A memorandum of how to write pandas that I tend to forget personally
How to write a string when there are multiple lines in python
How to use hmmlearn, a Python library that realizes hidden Markov models
[Introduction to Python] How to write a character string with the format function
How to read a CSV file with Python 2/3
How to create a Python virtual environment (venv)
How to open a web browser from python
How to embed a variable in a python string
How to create a JSON file in Python
How to generate a Python object from JSON
How to add a Python module search path
Why does Python have to write a colon?
How to write a ShellScript Bash for statement
How to notify a Discord channel in Python
[Python] How to draw a histogram in Matplotlib
How to write a named tuple document in 2020
[Go] How to write or call a function
I want to write to a file with Python
How to write a ShellScript bash case statement
How to install Python
How to install python
How to write environment variables that you don't want to put on [GitHub] Python
How to install a Python library that can be used by pharmaceutical companies
20th Offline Real-time How to Write Problems in Python
How to convert / restore a string with [] in python
How to write a GUI using the maya command
[Python] How to draw a line graph with Matplotlib
How to set up a Python environment using pyenv
How to write string concatenation in multiple lines in Python
I want to write in Python! (2) Let's write a test
[Python] How to expand variables in a character string
How to build a Django (python) environment on docker
How to make a Python package using VS Code
[Python] A memo to write CSV vertically with Pandas
A program to write Lattice Hinge with Rhinoceros with Python
How to save a table scraped by python to csv
[Python] A convenient library that converts kanji to hiragana
Write code to Unit Test a Python web app
[Python] How to create a 2D histogram with Matplotlib
How to execute a command using subprocess in Python
How to build a Python environment on amazon linux 2
[Introduction to Python] How to write repetitive statements using for statements
[Python] How to call a c function from python (ctypes)
How to create a kubernetes pod from python code
[Python] How to draw a scatter plot with Matplotlib
How to use pip, a package management system that is indispensable for using Python