Python Code convention Guide
21587 ワード
Pythonコード会議のクリーンアップ
Naming Conventions
# Correct
def get_text()
# Wrong
def get_Text()
def Get_Text()
def Get_text()
# Correct
text
text_length
# Wrong
Text
text_Length
Text_length
# Correct
g_text
g_text_length
# Wrong
G_text
g_Text_length
g_text_Length
# Correct
dataset.py
text_style.py
# Wrong
Dataset.py
text_Style.py
# Correct
class VideoCapture()
class VideoWriter()
# Wrong
class videoCapture()
class Video_writer()
# Correct
PATH
WINDOW_SIZE
# Wrong
Path
window_SIZE
インデント
インデント
# Correct
x = y
# Wrong
x = y
Imports
# Correct:
import os
import sys
# Wrong
import sys, os
# Correct
from subprocess import Popen, PIPE
式、条件式のスペース
# Correct
spam(ham[1], {egg: 2})
# Wrong
spam( ham[ 1 ], { eggs: 2} )
# Correct:
foo = (0,)
# Wrong
bar = (0, )
# Correct
if x == 4: print x, y; x, y = y, x
# Wrong
if x == 4 : print x , y ; x , y = y , x
예외
# Correct
ham[1:9], ham[1:9:3], ham[:9:3], ham[1::3], ham[1:9:]
ham[lower:upper], ham[lower:upper:], ham[lower::step]
ham[lower+offset : upper+offset]
ham[: upper_fn(x) : step_fn(x)], ham[:: step_fn(x)]
ham[lower + offset : upper + offset]
# Wrong
ham[lower + offset:upper + offset]
ham[1: 9], ham[1 :9], ham[1:9 :3]
ham[lower : : upper]
ham[ : upper]
# Correct:
spam(1)
# Wrong:
spam (1)
# Correct:
dct['key'] = lst[index]
# Wrong:
dct ['key'] = lst [index]
# Correct:
x = 1
y = 2
long_variable = 3
# Wrong:
x = 1
y = 2
long_variable = 3
# Correct:
i = i + 1
submitted += 1
x = x*2 - 1
hypot2 = x*x + y*y
c = (a+b) * (a-b)
# Wrong:
i=i+1
submitted +=1
x = x * 2 - 1
hypot2 = x * x + y * y
c = (a + b) * (a - b)
関数のスペース
# Correct:
def complex(real, imag=0.0):
return magic(r=real, i=imag)
# Wrong:
def complex(real, imag = 0.0):
return magic(r = real, i = imag)
条件文の次の行で関数を呼び出す# Correct:
if foo == 'blah':
do_blah_thing()
# Wrong 1:
if foo == 'blah': do_blah_thing()
# Wrong 2:
if foo == 'blah': do_blah_thing()
for x in lst: total += x
while t < 10: t = delay()
# Wrong 3:
if foo == 'blah': do_blah_thing()
else: do_non_blah_thing()
try: something()
finally: cleanup()
do_one(); do_two(); do_three(long, argument,
list, like, this)
if foo == 'blah': one(); two(); three()
# Correct
do_one()
do_two()
do_three()
# Wrong
do_one(); do_two(); do_three()
コメント
x = x + 1 # Increment x
"""Return a foobang
Optional plotz says to frobnicate the bizbaz first.
"""
"""Return an ex-parrot."""
公式Pythonコード会議ガイドアドレスReference
この問題について(Python Code convention Guide), 我々は、より多くの情報をここで見つけました https://velog.io/@heaseo/Python-Code-conventionテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol