Python_PEP-0008要約_161221
Last update: 2016/12/21 21:00
原文確認日: 2016/12/21
https://www.python.org/de...
Memo
原書には番号はありませんが、識別用に番号をつけました
原文確認日以降の原文変更には追随するか不明です (2016/12/21)
Programming Recommendations節
1. Python実装独自の機能を使わない
例: CPythonの a+=b や a = a+b
.join()を使うこと
2. is か is notを使う
例: Noneとの比較
Singleton
=演算子は使わないこと
3. if x を使う
if x it not Noneにしない
4. if notを使う
not ... isを使わない
可読性が高いため
5. __eq__, __ne__, __lt__, __le__, __gt__, __ge__ を使う
functions.total_ordering() decorate提供toolがある
missing comparison methodsを生成
PEP 207についてなんたらかんたら
6. lambda expressionに関してdef表記を使う
assignment 表記にしない
例(悪い): f = lambda x : 2*x
例(良い): def f(x): return 2*x
7. Exceptionからexceptionsを引き出す
BaseExceptionから引き出さない
8. exception chainingを適切に使う
raise X from Yを使うべき
inner exceptionについてなんたらかんたら
9. Python2: raise ValueError('message')を使う
古い形式: raise ValueError, 'message'は使わない
10. 例外キャッチ時には特定の例外を記載する
例: except ImprotError:
except: だけにしない
11. キャッチした例外のname bindingにはPython 2.6で追加されたexplicit name binding syntaxを使う
例: except Exception as exc:
のこと?
This is the only syntax supported in Python 3
説明の矛盾 (Python 2.6とPython 3)?
12. OSのエラーキャッチ時はexplicit exception hierarchyを使う
Python 3.3で導入
error値のintrospectionにしない
13. try/exception節において、try節は必要最小限のコードにする
masking bugの回避
13. withを使う
resourceが特定のsectionに結びつく場合
try/finallyも良い
14. Context managersは個別の関数からコールされるべき
resourceの獲得と解放、以外の場合
with conn.begin_transaction():
with conn: でなく
15. returnの書き方に一貫性を持たせる
例1: 全部のreturnでexpressionを返す
return res
return None
例2: 全部のreturnで何も返さない
return
16. string methodsを使う
string moduleを使わない
string methodsはずっと早い
unicode stringsと同じAPIを使う
17. .startswith()と .endswith()を使う
string slicingを使わない
例(悪い): if foo[:3] == 'bar':
例(良い): if foo.starswith('bar')
18. Object type比較にはisinstance()を使う
直接的なtype比較はしない
例(悪い): if type(obj) is type(1):
例(良い): if isinstance(obj, int):
19. sequencesに対して、empty sequencesはfalseという事実を利用する
sequences()
strings, lists, tuples
例(良い): if not seq:
例(悪い): if len(seq):
20. trailing whitespaceを多く持つstring literalsは使わない
可視化しにくい
Editorが取り除く(trimする)場合あり
21. bool値とTrue/Falseを==比較しない
例(良い): if greeting:

Created using MindMup.com