pylint
pylint
檢查 程式碼pip install pylint
安裝pylint XXX.py
執行pylint
並非完美, 僅是一個輔助工具. 你應該事情況
module_name
, package_name
, ClassName
, method_name
, ExceptionName
, function_name
, GLOBAL_CONSTANT_NAME
, global_var_name
, instance_var_name
, function_parameter_name
, local_var_name
a
, b
, c
)
i
, j
, k
-
)
calculate-histogram.py
__double_leading_and_trailing_underscore__
internal
: 僅使用於某module 或 以protected/private的形式存於某class的 變數或函示_
): 僅 慣例上代表, 該 變數或函示 為 internal 使用
import * from
時不會出現__
): 對 編譯器interpreter 有實際意義, 將使 變數或函示 變成 internal
舉下面例子, ref: https://shahriar.svbtle.com/underscores-in-python
>>> class A(object):
... def _internal_use(self):
... pass
... def __method_name(self):
... pass
...
>>> dir(A())
['_A__method_name', ..., '_internal_use']
可以發現 前綴雙底線 __method_name
將被編譯器 自動取代成 _A__method_name
這在處理 繼承 inherit 時是有幫助的
>>> class B(A):
... def __method_name(self):
... pass
...
>>> dir(B())
['_A__method_name', '_B__method_name', ..., '_internal_use']
CapWords
, module 名稱 使用 lower_with_under.py
from StringIO import StringIO
的尷尬情況Type | Public | Internal |
---|---|---|
Packages | snake_case |
|
Modules | snake_case |
_snake_case |
Classes | CapWords |
_CapWords |
Exceptions | CapWords |
|
Functions | snake_case() |
_snake_case() |
Global/Class Constants | CAPS_WITH_UNDER |
_CAPS_WITH_UNDER |
Global/Class Variables | lower_with_under |
_snake_case |
Instance Variables | snake_case |
_snake_case (protected) or __snake_case (private) |
Method Names | snake_case() |
_snake_case() (protected) or __snake_case() (private) |
Function/Method Params | snake_case |
|
Local Variables | snake_case |
spaces
tabs
跟 spaces
混用spaces
做縮排開頭YES: # Aligned with opening delimiter
foo = long_function_name(var_one, var_two,
var_three, var_four)
# Aligned with opening delimiter in a dictionary
foo = {
long_dictionary_key: value1 +
value2,
...
}
# 4-space hanging indent; nothing on first line
foo = long_function_name(
var_one, var_two, var_three,
var_four)
# 4-space hanging indent in a dictionary
foo = {
long_dictionary_key:
long_dictionary_value,
...
}
NO: # Stuff on first line forbidden
foo = long_function_name(var_one, var_two,
var_three, var_four)
# 2-space hanging indent forbidden
foo = long_function_name(
var_one, var_two, var_three,
var_four)
# No hanging indent in a dictionary
foo = {
long_dictionary_key:
long_dictionary_value,
...
}