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

第4章. デコレーター

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

Pythonはデコレーターと呼ばれる強力なツールをサポートしている。 デコレーターを使えば、関数やメソッドにまったく手を加えることなく、そのグループに豊富な機能を追加することができる。また、他の方法では不可能な方法で、コード内の異なる、イライラするほど絡み合った関心事をほぐしたり、強力で拡張可能なソフトウェアフレームワークを構築したりすることができる。世界で最も人気があり重要なPythonライブラリの多くはデコレーターを活用している。この章では、その方法を紹介する。

デコレーターとは、関数やメソッドに適用するものだ。デコレーターは見たことがあるだろう。property というデコレーターがあり、クラスでよく使われている:

class Person:
    def __init__(self, first_name, last_name):
        self.first_name = first_name
        self.last_name = last_name

    @property
    def full_name(self):
        return self.first_name + " " + self.last_name
>>> person = Person("John", "Smith")
>>> print(person.full_name)
John Smith

person.full_name() ではなく、person.full_name を印刷していることに注意してほしい。

別の例として、FlaskのWebフレームワーク 、シンプルなホームページを定義する方法を紹介しよう:

@app.route("/")
def hello():
    return "<html><body>Hello World!</body></html>"

app.route("/") はデコレーターで、ここではhello() という関数に適用されている。 つまり、ルートURL("/")へのHTTP GETリクエストは、hello() 関数によって処理される。

デコレーターは、関数の周囲に振る舞いを追加することで機能する。1-つまり、関数が始まる前、関数が戻った後、あるいはその両方で実行されるコードの行を追加する。関数内部のコード行を変更することはない。通常、わざわざデコレーターを定義する場合、少なくとも2つの異なる関数、通常はそれ以上の関数で使うつもりだろう。そうでなければ、デコレーターをわざわざ書かなくても、単独の関数の中に余分なコードを書くだけだ。

デコレーターの使い方はシンプルで簡単なので、プログラミングに慣れていない人でもすぐに使いこなせるようになる。この章の目的はそれとは異なり、さまざまな便利なデコレータを自分で書けるようになることだ。これは初級者向けのテーマではなく、かろうじて中級者向けと言える。デコレータを書くには、Pythonのいくつかの洗練された機能と、それらがどのように連携するかを深く理解する必要がある。ほとんどのPython開発者は、デコレータの作成方法を学ぶことはない。この章では、以下のことを学ぶ。

ベーシック・デコレーター

いったんデコレーターを書けば、それを使うのは簡単だ。 関数を定義する前の行に、@ とデコレーター名を書くだけだ:

@some_decorator
def some_function
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