Skip to Content
ロバストPython ―クリーンで保守しやすいコードを書く
book

ロバストPython ―クリーンで保守しやすいコードを書く

by Patrick Viafore, 鈴木 駿, 長尾 高弘
March 2023
Intermediate to advanced
384 pages
5h 30m
Japanese
O'Reilly Japan, Inc.
Content preview from ロバストPython ―クリーンで保守しやすいコードを書く

5章コレクション型

Pythonでは、コレクション型と格闘しない限り大したことはできない。コレクション型は、ユーザのリストやレストランと住所の対応関係といったデータの集まりを格納する。他のデータ型(たとえば、intfloatboolなど)が単一の値を対象とするのに対し、コレクション型は任意の数の値を格納できる。Pythonには、辞書、リスト、集合といったコレクション型がある。文字列もコレクションの一種であり、文字のシーケンスを格納する。しかしながら、初見のコードを読む際はどのコレクションが使われているのか判別が難しくなることがある。コレクション型が異なれば、動作も異なる。

「1章 ロバストPython入門」では、コレクション型の違いについて簡単に触れた。そこで取り上げたのは、ミュータブルかどうか、イテラブルかどうか、インデックス参照の有無や方法である。しかし、適切なコレクション型の選択は第一歩に過ぎない。コレクション型が暗黙的に持つ意味を理解し、コードを利用する開発者がそれを推論できなければならない。標準のコレクション型では不十分で独自のコレクション型を作る状況も認識する必要がある。最初は未来のメンテナに自分がどのコレクション型を選択したかを伝える方法からだ。おなじみの型アノテーションを使うのだ。

5.1 コレクションの型アノテーション

非コレクション型の型アノテーションについてはすでに説明したが、コレクション型の型アノテーションはまだ説明していない。幸い、すでに学んだ型アノテーションと大差はない。

具体例として、レシピ本アプリについて考える。手持ちのレシピ本をデジタルな形でまとめ、料理名、材料、著者から検索したい。著者が何冊のレシピ本を書いているかは、レシピ本のコレクションで当然知りたいことの1つだ。 ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

Rustの練習帳 ―コマンドラインツールの作成を通してRustを学ぶ

Rustの練習帳 ―コマンドラインツールの作成を通してRustを学ぶ

Ken Youens-Clark, 中山 光樹
scikit-learn、Keras、TensorFlowによる実践機械学習 第2版

scikit-learn、Keras、TensorFlowによる実践機械学習 第2版

Aurélien Géron, 下田 倫大, 長尾 高弘
Node.jsデザインパターン 第2版

Node.jsデザインパターン 第2版

Mario Casciaro, Luciano Mammino, 武舎 広幸, 阿部 和也
サイトリライアビリティワークブック ―SREの実践方法

サイトリライアビリティワークブック ―SREの実践方法

Betsy Beyer, Niall Richard Murphy, David K. Rensin, Kent Kawahara, Stephen Thorne, 澤田 武男, 関根 達夫, 細川 一茂, 矢吹 大輔, 玉川 竜司

Publisher Resources

ISBN: 9784814400171Other