Movatterモバイル変換


[0]ホーム

URL:


コンテンツにスキップ

関数

関数を定義するにはdef キーワードを使います。

#!/usr/bin/env pythondefanother_function():print('Another function')defmain():print('Hello, World!')another_function()if__name__=='__main__':main()

関数名は自由に与えることができますが、慣習的に英数字かつsnake_case (小文字を_ でつなぐ記法)で表記します。

Note

関数定義の間は慣習的に 2 行開けるルールになっています。

引数

関数には引数を渡すことができます。

defanother_function(x,y):print(f'The value of x is{x}')print(f'The value of y is{y}')defmain():another_function(5,6)

デフォルト引数

関数には引数を明示的に渡さなかった場合にデフォルト値を暗黙的に渡す機能があります。

defanother_function(x,y=10):print(f'The value of x is{x}')print(f'The value of y is{y}')defmain():another_function(5,3)# x: 5, y: 3another_function(7)# x: 7, y: 10

デフォルト引数は引数内で一番最後に渡す必要があります。

# OKdeff1(x,y=10):print(f'The value of x is{x}')print(f'The value of y is{y}')# OKdeff2(x,y=10,z=20):print(f'The value of x is{x}')print(f'The value of y is{y}')print(f'The value of z is{z}')# NGdeff3(y=10,x,z=20):print(f'The value of x is{x}')print(f'The value of y is{y}')print(f'The value of z is{z}')

Note

デフォルト引数の定義は慣習的に= の両端にはスペースを入れずに書きます。

キーワード引数

関数を呼び出すときに引数名を指定すれば引数を順不同で渡すこともできます。

defanother_function(x,y):print(f'The value of x is{x}')print(f'The value of y is{y}')defmain():another_function(y=10,x=20)

*args, **kwargs

関数の引数名に* が付いたものがあると、その変数は複数の引数を 1 つのタプルとして受け取るようになります。

defanother_function(x,*args):print(f'The value of x is{x}')# x: 0print(f'The value of args is{args}')# args: (1, 2, 3)defmain():another_function(0,1,2,3)

また引数名に** を付けると複数のキーワード引数を 1 つの辞書として受け取るようになります。

defanother_function(x,**kwargs):print(f'The value of x is{x}')# x: 0print(f'The value of kwargs is{kwargs}')# kwargs: {'y': 10, 'z': 20}defmain():another_function(0,y=10,z=20)

*args, **kwargs は併用することも可能です。

defanother_function(x,*args,**kwargs):print(f'The value of x is{x}')# x: 0print(f'The value of args is{args}')# args: (1, 2)print(f'The value of kwargs is{kwargs}')# kwargs: {'y': 10, 'z': 20}defmain():another_function(0,1,2,y=10,z=20)

Note

* 引数と** 引数はどのような名前にしても構いませんが、慣習的に*args, **kwargs が使われます。

引数のアンパック

関数に引数を渡すときにタプルに* を付けて渡すとタプルの各要素を個別の引数として渡せるようになります。

defanother_function(x,y,z):print(f'The value of x is{x}')# x: 0print(f'The value of y is{y}')# y: 1print(f'The value of z is{z}')# z: 2defmain():args=(0,1,2)another_function(*args)# another_function(0, 1, 2)

また辞書に** を付けて渡すと辞書の各要素をキーワード引数として渡せるようになります。

defanother_function(x,y,z):print(f'The value of x is{x}')# x: 0print(f'The value of y is{y}')# y: 1print(f'The value of z is{z}')# z: 2defmain():kwargs={'x':0,'y':1,'z':2}another_function(**kwargs)# another_function(x=0, y=1, z=2)

型ヒント

引数に特定の型だけを渡せるようにしたければ型ヒント を使用します。

defanother_function(x:int,y:int):print(f'The value of x * y is{x*y}')

型ヒントを使用しなければどんな型の変数も渡せます。これはしばしば混乱の元となるので、なるべく型ヒントを使うようにしましょう。

戻り値

関数の呼び出し側に値を返却する場合はreturn を使用します。

defplus_one(x):returnx+1defmain():x=plus_one(5)# x: 6print(f'The value of x is{x}')

戻り値として返す型は複数あっても構いません。

defanother_function(x):ifx==0:returnx+3.14else:returnx+1

この関数はx0 のときはfloat 型を返し、そうでないときはint 型を返します。複数の型を返すことを想定してなければ、型ヒントを使うことで誤って意図しない型を返却してしまうことを防ぐことができます。

defplus_one(x:int)->int:returnx+1

Python の関数は必ず戻り値を持っており、return を明示的に使わなかった場合はNone が返ります。

deff(x):print(f'The value of x is{x}')

この関数は下記と同じ意味になります。

deff(x):print(f'The value of x is{x}')returnNone

空実装

関数の実装を空にしたい場合はpass と書いておきます。

# 何もしない関数defempty_function():pass

[8]ページ先頭

©2009-2025 Movatter.jp