何かを書き留める何か

数学や読んだ本について書く何かです。最近は社会人として生き残りの術を学ぶ日々です。

Effective Python

『Effective Python』Item 22: 辞書やタプルで保持するよりもヘルパークラスを使おう

『Effective Python』の続き。リファクタリング! Effective Python › The Bookwww.effectivepython.com Pythonの辞書は便利だなあと思いつつ、次から次へと湧いてくる仕様変更・追加機能に対応していくと酷い目に合う。 生徒の成績を管理するコンテナを作る…

『Effective Python』Item 21: キーワード専用引数で関数をわかりやすくしよう

『Effective Python』の続き。Chapter2.の最後を飾るのはキーワード専用引数。 Effective Python › The Bookwww.effectivepython.com 割り算で、オーバーフローとかゼロ除算を無視して扱いたい、という場合、以下の様な気のきいたオプション付きの函数が書け…

『Effective Python』Item 20: 動的デフォルト引数ではNoneとドキュメンテーション文字列を使おう

『Effective Python』の続き。デフォルト引数の評価は作成時一度のみ。繰り返しませんのでどうか皆様Noneを使ってdocstringをお書きください。 Effective Python › The Bookwww.effectivepython.com 前回『Effective Python』Item 19: キーワード引数でオプ…

『Effective Python』Item 19: キーワード引数でオプションを提供しよう

『Effective Python』の続き。すごいぞキーワード変数。 Effective Python › The Bookwww.effectivepython.com 筆者はキーワード引数の利点として3つあげている。 函数の呼び出しの意図が明確になるから。 函数定義時にデフォルト値を定義できるから。 後方…

『Effective Python』Item 18: 可変長引数で視覚的ノイズを減らそう

『Effective Python』の続き。*argの使い道。 Effective Python › The Bookwww.effectivepython.com Pythonの函数の引数に*をつけると可変長引数となる。Item 18では2つ注意点をあげている。 まず、*をつけるとタプルとして展開される点である。 例えば、次…

『Effective Python』Item 17: 引数によるイテレーションは防御的に行う

『Effective Python』の続き。イテレータは使い捨て。 Effective Python › The Bookwww.effectivepython.com イテレータは状態を持っている(ステートフル)ので、「使い終わった」という状態が存在する。また、「使い終わった」と「元々何も無い」が区別で…

『Effective Python』Item 16: リストを返す代わりにジェネレータを検討しよう

『Effective Python』の続き。ジェネレータ便利みんな使おう。 Effective Python › The Bookwww.effectivepython.com まだ翻訳したのはItem 16までであるが、『Effective Python』には大きく2つのテーマがあると思う。 ビジュアルノイズを減らせ メモリ不足…

『Effective Python』Item 15: クロージャと変数スコープの関係を知ろう

『Effective Python』の続き。Pythonのスコープを勉強しなおした。 Effective Python › The Bookwww.effectivepython.com クロージャを理解しようとして書いたのが次の実用性に欠けるコードである。 def outer(out): def middle(mid): def inner(inn): print…

『Effective Python』Item 14: Noneを返すよりも例外を発生させよう

『Effective Python』の続き。第二章突入。 Effective Python › The Bookwww.effectivepython.com 第二章は函数の扱い方。返り値にNoneというのはやったことがないが、結構使われているのだろうか。 何が問題なのかは次のコードで一発だと思う。 def divide(…

『Effective Python』Item 13: try/except/else/finallyブロックを活用しよう

『Effective Python』の続き。for/elseとtry/except/else/finallyのelse`の扱いの違いとは Effective Python › The Bookwww.effectivepython.com タイトル通り、try/except/else/finallyをフル活用しようぜ、という話。 try/finallyは確実に終了処理をしたい…

『Effective Python』Item 12: forやwhileループ後のelseブロックは避ける

『Effective Python』の続き。for/else、while/elseダメ絶対 Effective Python › The Bookwww.effectivepython.com Python 言語リファレンス — Python 3.4.2 ドキュメントを読むと、for文やwhile文の後にelse節を追加できることが書いてある。 筆者は、いか…

『Effective Python』Item 11: 複数のイテレータを同時に処理するならzipを使おう

『Effective Python』の続き。そろそろAmazonのリンクは目障りなので外す。アフェリエイトなどやっていないのでね…。 Effective Python › The Bookwww.effectivepython.com 互いに関連している複数のリストを処理する際に、 names = ['Cecilia', 'Lise', 'Ma…

『Effective Python』Item 10: rangeよりもenumerateを使おう

『Effective Python』の続き。 Effective Python: 59 Specific Ways to Write Better Python (Effective Software Development Series)作者: Brett Slatkin出版社/メーカー: Addison-Wesley Professional発売日: 2015/03/08メディア: ペーパーバックこの商品…

『Effective Python』Item 9: 大きなリスト内包表記ではジェネレータ式を検討しよう

『Effective Python』の続き。みんな大好きリスト内包表記第三弾。 Effective Python: 59 Specific Ways to Write Better Python (Effective Software Development Series)作者: Brett Slatkin出版社/メーカー: Addison-Wesley Professional発売日: 2015/03/…

『Effective Python』Item 8: リスト内包表記では2つ以上の式を避ける

『Effective Python』の続き。邦訳は出るのですか。 Effective Python: 59 Specific Ways to Write Better Python (Effective Software Development Series)作者: Brett Slatkin出版社/メーカー: Addison-Wesley Professional発売日: 2015/03/08メディア: ペ…

『Effective Python』Item 7: mapやfilterの代わりにリスト内包を使おう

『Effective Python』の続き。まさかの連投。 Effective Python: 59 Specific Ways to Write Better Python (Effective Software Development Series)作者: Brett Slatkin出版社/メーカー: Addison-Wesley Professional発売日: 2015/03/08メディア: ペーパー…

『Effective Python』Item 6: 単一のスライスではstart, end, strideの同時利用を避ける

『Effective Python』の続き。Item 5の続きでもある。 Effective Python: 59 Specific Ways to Write Better Python (Effective Software Development Series)作者: Brett Slatkin出版社/メーカー: Addison-Wesley Professional発売日: 2015/03/08メディア: …

『Effective Python』Item 5: シーケンスのスライス方法を知ろう

『Effective Python』の続き。翻訳はでるのだろうか。 Effective Python: 59 Specific Ways to Write Better Python (Effective Software Development Series)作者: Brett Slatkin出版社/メーカー: Addison-Wesley Professional発売日: 2015/03/08メディア: …

『Effective Python』Item 4: 複雑な式の代わりにヘルパー関数を使おう

『Effective Python』の続き。続いた。 Effective Python: 59 Specific Ways to Write Better Python (Effective Software Development Series)作者: Brett Slatkin出版社/メーカー: Addison-Wesley Professional発売日: 2015/03/08メディア: ペーパーバック…

『Effective Python』Item 3: bytes、str、unicodeの違いを理解しよう

『Effective Python』の続き。 更新が遅いのは訳してから感想を書くことにしているからであるが、別に翻訳を公開するわけじゃないから…と思いつつ。 Effective Python: 59 Specific Ways to Write Better Python (Effective Software Development Series)作…

『Effective Python』Item 2: PEP8スタイルガイドに沿って書こう

『Effective Python』の続き。 Effective Python: 59 Specific Ways to Write Better Python (Effective Software Development Series)作者: Brett Slatkin出版社/メーカー: Addison-Wesley Professional発売日: 2015/03/08メディア: ペーパーバックこの商品…

『Effective Python』Item 1: 利用しているPythonのバージョンを知る

紀伊國屋から注文していたBrett Slatkin『Effective Python』が届いた。 Effective Python: 59 Specific Ways to Write Better Python (Effective Software Development Series)作者: Brett Slatkin出版社/メーカー: Addison-Wesley Professional発売日: 201…