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

9章データクラス

データクラスは、関連するデータを1つにまとめられるユーザ定義型である。整数、文字列、列挙型などの多くのデータ型はスカラーであり、単一の値だけを表現する。リスト、集合、辞書などは、主に同種コレクションを表す。しかし、これらとは別に、単一のデータ型に複数のデータフィールドを組み込みたい場合がある。辞書やタプルなら可能だが、これらにはまだ問題がある。実行時に辞書やタプルに何が格納されているかわかりにくいことがあり、コードが読みにくくなるのだ。そのため、コードを読んだりレビューしたりする際に推論が難しくなることがある。これはロバストネスを大きく損ねる。

データがどういうものかがわかりにくいと、読み手は前提条件を読み誤り、バグの発見が難しくなる。データクラスはこれらよりも読みやすく理解しやすい。そして、型チェッカはデータクラスの自然な扱い方を知っている。

9.1 データクラスの実際

データクラスは単一の複合型にまとめられた変数の異種コレクションを表す。複合型は複数の値から構成され、何らかの関連性や論理的なまとまりを表していなければならない。たとえば、Fraction(分数)は典型的な複合型である。Fractionには、numerator(分子)とdenominator(分母)の2個のスカラー値が含まれる。

from fractions import Fraction

Fraction(numerator=3, denominator=5)

このFractionは、numeratordenominatorの間の関係を表す。numeratordenominatorは互いに独立しており、片方を変えたからといってもう片方も変わることはない。しかし、これらをデータ型に結合することで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

Pythonからはじめるアルゴリズムトレード ―自動売買の基礎と機械学習の本格導入に向けたPythonプログラミング

Pythonからはじめるアルゴリズムトレード ―自動売買の基礎と機械学習の本格導入に向けたPythonプログラミング

Yves Hilpisch, 村上 振一郎
サイトリライアビリティワークブック ―SREの実践方法

サイトリライアビリティワークブック ―SREの実践方法

Betsy Beyer, Niall Richard Murphy, David K. Rensin, Kent Kawahara, Stephen Thorne, 澤田 武男, 関根 達夫, 細川 一茂, 矢吹 大輔, 玉川 竜司
スクラム実践者が知るべき97のこと

スクラム実践者が知るべき97のこと

Gunther Verheyen, 吉羽 龍太郎, 原田 騎郎, 永瀬 美穂

Publisher Resources

ISBN: 9784814400171Other