Python 返回语句

Python 返回语句用于返回函数中的值. 我们只能在函数中使用返回语句. 它不能在 Python 函数之外使用。

Python 函数无返回语句

Python中的每个函数都会返回一些东西,如果函数没有任何返回语句,则返回None

1def print_something(s):
2    print('Printing::', s)
3
4output = print_something('Hi')
5
6print(f'A function without return statement returns {output}')

输出:

Python Function Without Return Statement

Python 返回声明示例

我们可以在函数中执行一些操作,并使用返回语句将结果返回召唤者。

1def add(x, y):
2    result = x + y
3    return result
4
5output = add(5, 4)
6print(f'Output of add(5, 4) function is {output}')

输出:

Python Return Example

Python 返回表达式

我们也可以在返回陈述中有表达式,在这种情况下,表达式被评估并返回结果。

1def add(x, y):
2    return x + y
3
4output = add(5, 4)
5print(f'Output of add(5, 4) function is {output}')

输出:

Python Return Statement With Expression

Python 返回 Boolean

我们将使用 bool()函数获取对象的 Boolean 值。

1def bool_value(x):
2    return bool(x)
3
4print(f'Boolean value returned by bool_value(False) is {bool_value(False)}')
5print(f'Boolean value returned by bool_value(True) is {bool_value(True)}')
6print(f'Boolean value returned by bool_value("Python") is {bool_value("Python")}')

输出:

Python Return Boolean

Python 返回字符串

我们可以使用 str()函数来获得对象的 string 表示。

1def str_value(s):
2    return str(s)
3
4print(f'String value returned by str_value(False) is {str_value(False)}')
5print(f'String value returned by str_value(True) is {str_value(True)}')
6print(f'String value returned by str_value(10) is {str_value(10)}')

输出:

Python Return String

Python 返回Tuple

有时我们想将某些变量转换为 tuple

1def create_tuple(*args):
2    my_list = []
3    for arg in args:
4        my_list.append(arg * 10)
5    return tuple(my_list)
6
7t = create_tuple(1, 2, 3)
8print(f'Tuple returned by create_tuple(1,2,3) is {t}')

输出:

Python Function Return Tuple

** 进一步阅读**: [Python *args 和 **kwargs]( / 社区 / 教程 / python-args-kwargs)

Python函数返回另一个函数

这种方法类似于 Currying,它是将一个函数的评估转换为使用多个参数来评估函数序列的技术,每个参数都具有单一参数。

 1def get_cuboid_volume(h):
 2    def volume(l, b):
 3        return l * b * h
 4
 5    return volume
 6
 7volume_height_10 = get_cuboid_volume(10)
 8cuboid_volume = volume_height_10(5, 4)
 9print(f'Cuboid(5, 4, 10) volume is {cuboid_volume}')
10
11cuboid_volume = volume_height_10(2, 4)
12print(f'Cuboid(2, 4, 10) volume is {cuboid_volume}')

输出:

Python Return Function

Python函数返回外部函数

还可以返回函数,该函数在函数外定义的函数返回语句。

 1def outer(x):
 2    return x * 10
 3
 4def my_func():
 5    return outer
 6
 7output_function = my_func()
 8print(type(output_function))
 9
10output = output_function(5)
11print(f'Output is {output}')

输出:

Python Function Return Outer Function

Python 返回多个值

如果您想要返回函数的多个值,您可以返回 tuple、 listdictionary对象,根据您的要求。但是,如果您必须返回大量的值,那么使用序列是太多的资源收集操作。我们可以使用 yield,在这种情况下,返回多个值一个接一个。

 1def multiply_by_five(*args):
 2    for arg in args:
 3        yield arg * 5
 4
 5a = multiply_by_five(4, 5, 6, 8)
 6
 7print(a)
 8# showing the values
 9for i in a:
10    print(i)

输出:

Python Return vs Yield

摘要

Python 返回语句用于返回函数的输出,我们了解到我们也可以返回另一个函数的函数,还可以评估表达式,然后返回函数的结果。

您可以从我们的 GitHub 存储库中查阅完整的 Python 脚本和更多 Python 示例。

Published At
Categories with 技术
Tagged with
comments powered by Disqus