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

第6章 クラスとオブジェクト クラスとオブジェクト:基本を超えて

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

この章では、Pythonにおけるオブジェクト指向プログラミング(OOP)の基本(クラスの作成、メソッドの定義、単純な継承)に慣れていることを前提とする。この章では、その知識を基礎にしていく。

どのオブジェクト指向言語でもそうだが、デザインパターン-クラスやオブジェクトに関わる一般的な問題に対する再利用可能な解決策-について学ぶことは有益だ。 デザインパターンについては多くのことが書かれている。そしてその多くはPythonにも当てはまるが、適用される傾向が異なる

というのも、多くのデザインパターンの本や記事は、Java、C++、C#といった言語向けに書かれているからだ。しかし、言語としてのPythonは違う。 動的型付け、ファーストクラス関数、その他の追加機能はすべて、「標準的な」デザインパターンが異なる形で機能することを意味する。

では、Python OOPの本当の意味を学ぼう。

プロパティ

Pythonオブジェクトには属性がある。"属性 "とは一般化された用語で、x.yz.f() のような式の中で "ドットの右側にあるもの "を意味する。メンバー変数とメソッドは属性の2種類だ。しかし、Pythonにはプロパティと呼ばれる別の種類の属性がある。

プロパティとは、メソッドとメンバ変数のハイブリッドである。このアイデアは、外部からはメンバ変数のように振る舞う属性を作成することだが、この属性への読み取りや書き込みは内部的にメソッド呼び出しをトリガーする。

@property という特殊化デコレーターでセットする。簡単な例だ:

class Person:
    def __init__(self, firstname, lastname):
        self.firstname = firstname
        self.lastname = lastname

    @property
    def fullname(self):
        return self.firstname + " " + self.lastname

これをインスタンス化することで、fullname にメンバー変数のようにアクセスできる:

>>> joe = Person("Joe", "Smith")
>>> joe.fullname
'Joe Smith'

ここで実際のメンバ変数を注意深く探す。firstnamelastname の2つがコンストラクタにセットされている。このクラスにはfullname というメソッドもある。しかし、インスタンスを作成した後、joe.fullname をメンバ変数として参照している。joe.fullname() をメソッドとして呼び出しているわけではない。しかし、joe.fullname の値を読み取ると、fullname() メソッドが呼び出される。

これはすべて、@property デコレーターによるものだ。このデコレーターをメソッドに適用すると、メソッドとしてアクセスできなくなる。 メンバ変数のようにアクセスしなければならない。実際、メソッドとして呼び出そうとするとエラーになる:

>>> joe.fullname()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError ...
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