Skip to Content
『Learning Python 第6版』
book

『Learning Python 第6版』

by Mark Lutz
March 2025
Intermediate to advanced
1172 pages
24h 57m
Japanese
O'Reilly Media, Inc.
Content preview from 『Learning Python 第6版』

第14章. 反復と包括

この作品はAIを使って翻訳されている。ご意見、ご感想をお待ちしている:translation-feedback@oreilly.com

前の章では、Pythonの2つのループ文、while とfor を紹介した。Pythonのループ文は、プログラムが実行する必要があるほとんどの反復タスクを処理することができるが、コレクションに対する反復処理は非常に一般的で普及しているため、Pythonはそれをよりシンプルで効率的にするための追加のツールを提供している。この章では、これらの。具体的には、Pythonの反復プロトコルを紹介し、for ループで使用されるメソッド呼び出しモデルを紹介し、for ループに近い、コレクション内の各アイテムに式を適用する内包について詳細を説明する。

これらのツールは、for ループと関数の両方に関連しているため、本書では、あとがきとともに2回に分けて取り上げることにする:

  • この章では、前章の続きとして、ループ系ツールの基本を紹介する。

  • 第20章では、関数ベースのツールの文脈でそれらを再検討し、組み込みとユーザ定義のジェネレータを含むようにトピックを拡張する。

  • 第30章では、ユーザ定義の反復可能オブジェクトをクラスでコーディングする方法を紹介する。

前置きが長くなったが、この章で紹介されている概念のいくつかは、一見すると高度に見えるかもしれない。しかし練習すれば、これらのツールが便利で自然なものであることが発見できるだろう。厳密には決して必要ではないが、Pythonのコードでは当たり前になっているので、他の人が書いたプログラムを読まなければならない場合、基本的な理解が助けになる。

反復

前章で、 for ループは、リスト、タプル、文字列など、Pythonのあらゆるシーケンス型に対して、次のように動作することを学んだ(複合文の後のREPLが必要とする空行は、簡潔にするために省略した):

>>> for x in [1, 2, 3, 4]: print(x ** 2, end=' ')
1 4 9 16

>>> for x in (1, 2, 3, 4): print(x ** 3, end=' ')
1 8 27 64

>>> for x in 'text': print(x * 2, end=' ')
tt ee xx tt

これも学んだことだが、for ループはこれよりもさらに汎用的で、どんな反復可能オブジェクトでも動作する。実際、これはPythonでオブジェクトを左から右へスキャンするすべての反復ツールに当てはまる。for ループ、あらゆる種類の内包、in メンバーシップ・テスト、zip 、 enumerate 、map 組み込み関数などがある。反復可能なオブジェクトであれば、辞書のような非シーケンスでも構わない:

>>> for k in dict(a=1, b=2, c=3): print(k, end=' ')
a b c

反復可能なオブジェクトという概念は、Pythonが誕生した後に、Pythonのツールセットに浸透してきた。これは基本的にシーケンスの概念を一般化したもので、オブジェクトが物理的に格納されたシーケンスであるか、for のような反復ツールのコンテキストで一度に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

Python Polars ―高速なデータ処理を実現する次世代ライブラリ

Python Polars ―高速なデータ処理を実現する次世代ライブラリ

Jeroen Janssens, Thijs Nieuwdorp, 中田 秀基
入門 Python 3 第2版

入門 Python 3 第2版

Bill Lubanovic, 鈴木 駿, 長尾 高弘

Publisher Resources

ISBN: 9798341634367