Skip to Content
『Learning Python 第6版』
book

『Learning Python 第6版』

by Mark Lutz
March 2025
Intermediate to advanced
1172 pages
24h 57m
Japanese
O'Reilly Media, Inc.
Content preview from 『Learning Python 第6版』

第16章. 関数の基本

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

パートIIIでは、Pythonの基本的な手続き文について学んだ。ここでは、独自の関数を作成するために使用できる、追加的な文と式のセットについて説明する。

簡単に言えば、関数とは、名前で呼び出されるコードのパッケージである。プログラム内で複数回実行できるように、、一連の文にラベルを付けてグループ化する。関数はまた、結果値を計算することもできるし、入力となるパラメータを指定することもできる。演算子を関数で包むことで、関数は一般的に便利なツールとなり、さまざまな場面で活用できる。

より実用的なレベルでは、関数はカット・アンド・ペーストによるプログラミングに代わるものである。演算子のコピーを何重にも冗長化するのではなく、単一の関数にまとめることができる。そうすることで、将来の作業を根本的に減らすことができる。演算子を後で変更しなければならない場合、プログラム中に散在している多くのコピーではなく、関数内で更新する1つのコピーだけでよいのだ。

関数はまた、Pythonがコード再利用を最大化するために提供する最も基本的なプログラム構造であり、プログラム設計のより大きな概念へと導いてくれる。これからわかるように、関数は複雑なシステムを管理しやすい部分に分割してくれる。各部分を関数として実装することで、再利用可能になり、コードも簡単になる。

このセットには、呼び出し式、関数を作成する2つの方法(def とlambda )、スコープの可視性を管理する2つの方法(global とnonlocal )、呼び出し元に結果を返す2つの方法(return とyield )、結果を一時停止するツール(async とawait )が含まれる。機能豊富なトピックではあるが、よく使われる核心部分は単純であることが発見できるだろう。

なぜ関数を使うのか?

詳細に入る前に、関数とは何なのか、より明確にイメージしておこう。関数は、ほぼ普遍的なプログラミング構造化装置である。他の言語では、サブルーチンや プロシージャと ...

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

Fluent Python 第2版 ―Pythonicな思考とコーディング手法

Fluent Python 第2版 ―Pythonicな思考とコーディング手法

Luciano Ramalho, 牧野 聡
Fluent Python ―Pythonicな思考とコーディング手法

Fluent Python ―Pythonicな思考とコーディング手法

Luciano Ramalho, 豊沢 聡, 桑井 博之, 梶原 玲子

Publisher Resources

ISBN: 9798341634367