Pythonのリターンステートメント

Pythonのreturn文は、関数から値を返すために使用されます。return文は関数内でのみ使用でき、Pythonの関数の外では使用できません。

return文がないPythonの関数

Pythonのすべての関数は何かを返します。関数にreturn文がない場合、Noneが返されます。

def print_something(s):
    print('Printing::', s)


output = print_something('Hi')

print(f'A function without return statement returns {output}')

出力:

Python Function Without Return Statement

Pythonのreturn文の例

関数内で操作を行い、結果を呼び出し元に返すことができます。

def add(x, y):
    result = x + y
    return result


output = add(5, 4)
print(f'Output of add(5, 4) function is {output}')

出力:

Python Return Example

式を含むPythonのreturn文

return文では式も使用できます。その場合、式が評価され、結果が返されます。

def add(x, y):
    return x + y


output = add(5, 4)
print(f'Output of add(5, 4) function is {output}')

出力:

Python Return Statement With Expression

Pythonのブール値を返す

関数の引数のブール値を返す例を見てみましょう。オブジェクトのブール値を取得するためにbool()関数を使用します。

def bool_value(x):
    return bool(x)


print(f'Boolean value returned by bool_value(False) is {bool_value(False)}')
print(f'Boolean value returned by bool_value(True) is {bool_value(True)}')
print(f'Boolean value returned by bool_value("Python") is {bool_value("Python")}')

出力:

Python Return Boolean

Pythonの文字列を返す

引数の文字列表現を返す関数の例を見てみましょう。オブジェクトの文字列表現を取得するにはstr()関数を使用できます。

def str_value(s):
    return str(s)


print(f'String value returned by str_value(False) is {str_value(False)}')
print(f'String value returned by str_value(True) is {str_value(True)}')
print(f'String value returned by str_value(10) is {str_value(10)}')

出力:

Python Return String

Pythonのタプルを返す

時々、複数の変数をタプルに変換したいと思うことがあります。可変個の引数からタプルを返す関数の書き方を見てみましょう。

def create_tuple(*args):
    my_list = []
    for arg in args:
        my_list.append(arg * 10)
    return tuple(my_list)


t = create_tuple(1, 2, 3)
print(f'Tuple returned by create_tuple(1,2,3) is {t}')

出力:

Python Function Return Tuple

さらなる読み物: Python *args と **kwargs

Python 関数が別の関数を返す

return 文から関数を返すこともできます。これは、複数の引数を取る関数の評価を、各々単一の引数を持つ関数のシーケンスを評価するように変換する技術であるカリー化に類似しています。

def get_cuboid_volume(h):
    def volume(l, b):
        return l * b * h

    return volume


volume_height_10 = get_cuboid_volume(10)
cuboid_volume = volume_height_10(5, 4)
print(f'Cuboid(5, 4, 10) volume is {cuboid_volume}')

cuboid_volume = volume_height_10(2, 4)
print(f'Cuboid(2, 4, 10) volume is {cuboid_volume}')

出力:

Python Return Function

Python 関数が外部関数を返す

return 文で定義された関数を返すこともできます。

def outer(x):
    return x * 10


def my_func():
    return outer


output_function = my_func()
print(type(output_function))

output = output_function(5)
print(f'Output is {output}')

出力:

Python Function Return Outer Function

Python 複数の値を返す

もし関数から複数の値を返したい場合、必要に応じてタプル、リスト、または辞書オブジェクトを返すことができます。ただし、大量の値を返す必要がある場合、シーケンスを使用するとリソースを多く消費します。その場合、yieldを使用して複数の値を一つずつ返すことができます。

def multiply_by_five(*args):
    for arg in args:
        yield arg * 5


a = multiply_by_five(4, 5, 6, 8)

print(a)
 # 値を表示する
for i in a:
    print(i)

出力:

Python Return vs Yield

要約

Pythonのreturn文は、関数から出力を返すために使用されます。関数から関数を返すこともできることを学びました。また、式は評価され、その結果が関数から返されます。

詳細なPythonスクリプトや他のPythonの例は、GitHubリポジトリから確認できます。

Source:
https://www.digitalocean.com/community/tutorials/python-return-statement