Skip to Content
ファストAPI
book

ファストAPI

by Bill Lubanovic
March 2025
Intermediate to advanced
280 pages
3h 20m
Japanese
O'Reilly Media, Inc.
Content preview from ファストAPI

第5章. パイダンティック、タイプヒント、モデルツアー

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

Pythonタイプヒントを使用したデータ検証とセット管理。

高速で拡張可能なPythonは、あなたのリンター/IDE/頭脳とうまく連携する。 Python 3.6+でデータがどのようにあるべきかを定義し、Pythonでそれを検証する。

サミュエル・コルバン、パイダンティック開発者

プレビュー

FastAPIは、Pydanticと呼ばれるPythonパッケージが主な基盤となっている。 これは、モデル (Pythonオブジェクトクラス)を使用してデータ構造を定義する。これらはFastAPIアプリケーションで多用されており、大規模なアプリケーションを記述する際に大きな利点となる。

タイプ・ヒンティング

、Pythonの型ヒントについてもう少し学ぼう。

第2章では、多くのコンピュータ言語では、変数はメモリ上の値を直接指すと述べた。 そのため、プログラマはその型を宣言する必要があり、それによって値のサイズとビットを決定することができる。 Pythonでは、変数はオブジェクトに関連付けられた単なる変数名であり、型を持っているのはオブジェクトである。

標準的なプログラミングでは、変数は通常同じオブジェクトに関連付けられている。 その変数に型ヒントを関連付ければ、プログラミングの間違いを避けることができる。 そこでPythonは、標準の型付けモジュールに型ヒントを追加した。 Pythonインタプリタは型ヒントの構文を無視し、あたかもそれが存在しないかのようにプログラムを実行する。 では、何が言いたいのか?

ある行で変数を文字列として扱い、後で忘れて別の型のオブジェクトを代入してしまうかもしれない。 他の言語のコンパイラーは文句を言うだろうが、Pythonは文句を言わない。 Pythonの標準インタプリタは、通常の構文エラーや実行時例外はキャッチするが、変数の型が混在していることはキャッチしない。 mypyのようなヘルパーツールは、型のヒントに注意を払い、ミスマッチについて警告する。

また、ヒントはPython開発者が利用でき、型エラーチェック以上の処理を行うツールを書くことができる。 以下のセクションでは、Pydanticパッケージがどのように開発され、明らかでなかったニーズに対応したかを説明する。 その後、FastAPIとの統合により、Web開発の多くの問題がどのように処理しやすくなったかを説明する。

ところで、型ヒントはどのようなものだろうか。 変数用の構文と関数の戻り値用の構文がある。

変数タイプのヒント 型のみを含むことがある:

name: type

あるいは、変数の値を初期化することもできる:

name: type = value

型はintstr のようなPython標準の単純型か、tuplelistdict のようなコレクション型のいずれかである:

thing: str = "yeti"

Python 3.9以前では、型付けモジュールからこれらの標準型名の大文字バージョンをインポートする必要がある:

from typing import Str
thing: Str = "yeti"

以下は初期化した例である:

physics_magic_number: float = 1.0/137.03599913
hp_lovecraft_noun ...
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

GitHubアクションを学ぶ

GitHubアクションを学ぶ

Brent Laster
仕事ではじめる機械学習 第2版

仕事ではじめる機械学習 第2版

有賀 康顕, 中山 心太, 西林 孝

Publisher Resources

ISBN: 9798341624658