Skip to Content
Effective Python 第3版 ―Pythonプログラムを改良する125項目
book

Effective Python 第3版 ―Pythonプログラムを改良する125項目

by Brett Slatkin, 鈴木 駿
October 2025
Intermediate to advanced
572 pages
7h 46m
Japanese
O'Reilly Japan, Inc.
Content preview from Effective Python 第3版 ―Pythonプログラムを改良する125項目

13章テストとデバッグ

Pythonの動的な性質やデフォルトの静的型チェックの欠如は、祝福でもあり呪いでもあります†1[項目3]参照)。しかし、Pythonプログラマの大半は、Pythonの簡潔さとシンプルさによる生産性の向上を理由に使う価値があると述べています。一方で、Pythonプログラマの大半は、Pythonの実行時にとんでもないエラーに遭遇した経験を持っています。筆者が知っている最悪の経験は、モジュールの動的インポートの副作用が原因で、SyntaxErrorが本番環境で発生してサーバプロセスがクラッシュしたというものです([項目98]参照)。まさかの事態に見舞われたプログラマは、それ以来二度とPythonを使うのをやめてしまいました。

[†1] 訳注:見よ、私は今日、あなたがたの前に祝福と呪いを置く。もし、今日私が命じる、あなたがたの神、主の戒めに聞き従うならば祝福を、もし、あなたがたの神、主の戒めに聞き従わず、私が今日あなたがたに命じる道を外れ、あなたがたが知らなかった他の神々に従うならば、呪いを置く。(申命記 11章26‐28節、聖書協会共同訳)

しかし、どうして本番環境にデプロイする前に十分なテストを行わなかったのでしょうか。静的型付けがすべてではありません。どのようなプログラミング言語で書かれていようとも、必ずコードをテストしましょう。尤も、他のプログラミング言語に比べてPythonでは正しさを検証するためのテスト記述がより重要であると言えるでしょう。Pythonの動的な性質を活用すればコードのテストやデバッグも簡単にできます。

項目108 TestCaseでテストを実装する

Pythonでテストを記述する標準的な方法は、標準ライブラリのunittestを使う方法です。例として、次のようなユーティリティ関数に対するテストを考えます。 ...

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

Think Python 第3版 ―コンピュータ科学者の思考で問題を解決する

Think Python 第3版 ―コンピュータ科学者の思考で問題を解決する

Allen B. Downey, 大橋 真也
入門 Python 3 第2版

入門 Python 3 第2版

Bill Lubanovic, 鈴木 駿, 長尾 高弘

Publisher Resources

ISBN: 9784814401338Publisher Website