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 ―クリーンで保守しやすいコードを書く

13章プロトコル

告白しなければならないことがある。Python型システムの一見して矛盾している問題について話すのを避けてきたことだ。Pythonの実行時における型システムと静的な型ヒントの間の根本的な考え方の違いについて話さなければならない。

「2章 Pythonデータ型入門」では、Pythonがダックタイピングをサポートする仕組みを説明した。ダックタイピングとは、オブジェクトが特定の振る舞いをサポートする限り特定のコンテキストでそのオブジェクトを使ってよいということだ。ダックタイピングを使うために基底クラスやあらかじめ定義された継承関係はいらない。

しかし、型チェッカは何らかの支援が得られなければダックタイピングの正しさを判断できない。型チェッカは静的解析の時点でわかっているデータ型なら処理できるが、実行時に行われるダックタイピングには手が出ない。

この問題を緩和するために、Python 3.8で導入されたプロトコルについて説明する。プロトコルは今触れた矛盾を解決する。プロトコルは型チェックの過程でダックタイピングされた変数にアノテーションをつける。ここでは、プロトコルが必要な理由、独自プロトコルの定義方法、プロトコルの高度な使い方を説明する。しかし、まずPythonのダックタイピングと静的型チェッカの間の断絶を理解する必要がある。

13.1 2つの型システムの間の緊張関係

この章では、自動ランチショップのデジタルメニューシステムを作る。このランチショップのメニューには、「小盛」、つまり一人前の半分を注文できる料理が多数含まれる。デリサンド、ラップサンド、スープは小盛になるが、ドリンクやハンバーガーは小盛にできない。重複を取り除くために、すべての「小盛」処理をこなすメソッドがほしい。例として料理のクラスを考える。 ...

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