Skip to Content
強力なPython
book

強力なPython

by Aaron Maxwell
March 2025
Intermediate to advanced
200 pages
2h 51m
Japanese
O'Reilly Media, Inc.
Content preview from 強力なPython

第1章. ジェネレーターでスケーリングする

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

このfor のループは単純に見える:

for item in items:
    do_something_with(item)

しかし、奇跡はここに隠されている。ご存知のように、コレクションを一度に1要素ずつ呼び出すことを反復と呼ぶ。Pythonのイテレーションシステムが実際にどのように機能するのかを理解し、それがいかに深く、よく考え抜かれたものであるかを評価する人は少ない。この章を読めば、あなたもその一人になれる。スケーラビリティの高いPythonアプリケーションを書けるようになり、より大きなデータセットをパフォーマンスよく、メモリ効率のよい方法で扱えるようになる。

反復処理は、Pythonの最も強力なツールの1つであるジェネレーター関数の中核でもある。ジェネレータ関数は、便利なイテレータを作成する便利な方法というだけではない。 ジェネレータ関数は、その性質上、本質的に優れたコーディング習慣を促すような方法で、絶妙なパターンのコードの組織化を可能にする。

この章が特殊化するのは、この章を理解することで、どの言語でも永久に優れたプログラマになれる恐れがあるからだ。Pythonジェネレータをマスターすることは、その過程で得られる区別や洞察のために、そうなる傾向がある。 さあ、飛び込もう。

Pythonにおける反復処理

Pythonにはiter() という組み込み関数がある。この関数にコレクションを渡すと、イテレータオブジェクトが返ってくる:

>>> numbers = [7, 4, 11, 3]
>>> iter(numbers)
<list_iterator object at 0x10219dc50>

イテレータとは、シーケンスの値を1つずつ生成するオブジェクトのことである:

>>> numbers_iter = iter(numbers)
>>> for num in numbers_iter:
...     print(num)
7
4
11
3

通常はiter() を使う必要はない。その代わりにfor num in numbers と書くと、Pythonはそのコレクションに対してiter() を呼び出す。 これは自動的に行われる。呼び出されたオブジェクトは、for ループのイテレータとして使われる:

# This...
for num in numbers:
    print(num)

# ... is effectively just like this:
numbers_iter = iter(numbers)
for num in numbers_iter:
    print(num)

コレクションに対するイテレータは、それ自身のIDを持つ独立したオブジェクトであり、それはid() で確認できる:

>>> # id() returns a unique number for each object.
... # Different objects will always have different IDs.
>>> id(numbers)
4330133896
>>> id(numbers_iter)
4330216640

iter() 、実際にどうやってイテレータを取得するのだろうか?これにはいくつかのメソッドがあるが、ひとつは__iter__() というマジック・メソッドに ...

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

AWS上のレジリエントなシステムの構築

AWS上のレジリエントなシステムの構築

Kevin Schwarz, Jennifer Moran, Nate Bachmeier

Publisher Resources

ISBN: 9798341634039