Hello. Immediately, is the source code you wrote easy to read?
There are many opportunities for people other than yourself to see the code you wrote, for example, when co-developing with multiple people, publishing it on the Internet, or delivering it. If the code you write at such times is well-formatted, you will be able to understand it quickly and without misunderstandings, not only for the reader but also for reviewing your own code.
In such cases, the conventions used to format Python code are ** PEP 8 ** and ** PEP 257 **.
In this article, I will introduce some typical rules that can be understood casually and achieve the minimum appearance **, instead of strictly following the contents of PEP 8 and PEP 257. To do.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
First of all, this is often written at the beginning of the code. The first line is ** Shebang ** and the second line is called ** Magic Comment **.
** Shebang ** describes something like "Please run this file with this program".
By writing this, when you want to execute this code (temporarily named sample.py),
$ python sample.py
not
$ ./sample.py
You will be able to execute it with the command of.
Next is ** magic comment **, which according to PEP8 is ** unnecessary ** if it is Python3 source code and written in UTF-8.
The magic comment describes the character encoding of the written code, and it seems that it should not be written because the default character code is UTF-8 in Python3.
There are three main rules for import statements.
# OK
import os
import sys
# NG
import sys, os
from sklearn.model_selection import GridSearchCV, train_test_split
import os
import sys
from sklearn.model_selection import GridSearchCV, train_test_split
from mypackage import sample
Docstring
A description of the defined function or class. This is a lot of work to describe, but it looks different with and without it.
def my_func(x):
"""
Returns the squared value.
Parameters
----------
x : int
Target integer value
Returns
-------
y : int
Input squared value
"""
y = x ** 2
return y
Enclose the Docstring in ** three double quotes **. Single quotes are NG. You can write the content after the double quoted line.
#This is also OK
def my_func_hoge(x):
"""Returns the squared value.
Parameters
----------
x : int
Target integer value
Returns
-------
y : int
Input squared value
"""
y = x ** 2
return y
#Let's write at least a summary.
def my_func_fuga(x):
"""Returns the squared value."""
y = x ** 2
return y
On the first line, write a summary of the target function or class. As a caveat, make sure that the last character is a half-width period **.
Insert a blank line after the summary to enumerate the arguments, and after the argument, insert a blank line in the same way to enumerate the return values.
It seems that Jupyter etc. may be good if you write this, but the explanation here is omitted.
This is so-called indentation. Use ** 4 spaces ** instead of tabs. However, when using tensorflow, the indentation tends to be deep, so two spaces are acceptable.
#If the argument of the function becomes long, a line break is made according to the left parenthesis position.
a = long_name_function(hikisu_ichi, hikisu_ni,
hikisu_san, hikisu_yon)
#Lower the indent and start a new line
b = long_name_function(
hikisu_ichi, hikisu_ni,
hikisu_san, hikisu_yon)
#Line breaks with indentation lowered from the middle are NG
c = long_name_function(hikisu_ichi, hikisu_ni,
hikisu_san, hikisu_yon)
Line breaks in long formulas
#It is OK if it is unified whether to start a new line before or after the calculation formula
result_1 = (sushiki_ichi
+ sushiki_ni
- (sushiki_san - susiki_yon)
- susiki_go)
result_2 = (sushiki_ichi +
sushiki_ni -
(sushiki_san - susiki_yon) -
susiki_go)
In principle, leave two lines at the top level, that is, line breaks without indentation, and one or less lines otherwise.
# OK
hoge = {'fuga': [0]}
# NG
hoge = { 'fuga': [ 0 ] }
# OK
if a == b:
c = {'d': 1}
# NG
if a == b :
c = {'d' : 1}
#The exception is the colon used for slicing
array[2:5]
a = i + 1
b = c >= d
e = f not in g_list
h = j and k
#However, it is used to describe default parameters in functions, etc.=Do not leave space on both sides of
def hoge(piyo=None):
pass
#Used to describe arguments when specifying function keywords=Do not leave space on both sides of
piyopiyo = hoge(piyo=1)
x = (b + (b**2 - 4*a*c) ** 0.5) / (-2*a)
How was it? This time, I haven't covered all of PEP 8 and PEP 257, but I hope you can understand it roughly as to what you should pay attention to.
I would be very happy if you read this article and would like to pursue readable and good looking coding.
Recommended Posts