The python return statement is used to return values from the function. We can use the return statement in a function only. It can’t be used outside of a Python function.
python return语句用于从函数返回值。 我们只能在函数中使用return语句。 不能在Python函数之外使用。
没有return语句的Python函数 (Python Function without return statement)Every function in Python returns something. If the function doesn’t have any return statement, then it returns None.
Python中的每个函数都会返回一些信息。 如果该函数没有任何return语句,则它将返回None 。
def print_something(s):
print('Printing::', s)
output = print_something('Hi')
print(f'A function without return statement returns {output}')
Output:
输出:
Python Function Without Return Statement
没有返回语句的Python函数
Python返回语句示例 (Python Return Statement Example)We can perform some operation in a function and return the result to the caller using the return statement.
我们可以在函数中执行一些操作,然后使用return语句将结果返回给调用方。
def add(x, y):
result = x + y
return result
output = add(5, 4)
print(f'Output of add(5, 4) function is {output}')
Output:
输出:
Python Return Example
Python返回示例
带有表达式的Python return语句 (Python return statement with expression)We can have expressions also in the return statement. In that case, the expression is evaluated and the result is returned.
我们也可以在return语句中使用表达式。 在这种情况下,将对表达式求值并返回结果。
def add(x, y):
return x + y
output = add(5, 4)
print(f'Output of add(5, 4) function is {output}')
Output:
输出:
Python Return Statement With Expression
带表达式的Python返回语句
Python返回布尔值 (Python return boolean)Let’s look at an example where we will return the boolean value of the argument of a function. We will use bool() function to get the boolean value of the object.
让我们看一个示例,在该示例中,我们将返回函数参数的布尔值。 我们将使用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")}')
Output:
输出:
Python Return Boolean
Python返回布尔值
Python返回字符串 (Python return string)Let’s look at an example where our function will return the string representation of the argument. We can use the str() function to get the string representation of an object.
让我们看一个示例,其中我们的函数将返回参数的字符串表示形式。 我们可以使用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)}')
Output:
输出:
Python Return String
Python返回字符串
Python返回元组 (Python return tuple)Sometimes we want to convert a number of variables into a tuple. Let’s see how to write a function to return a tuple from a variable number of arguments.
有时我们想将多个变量转换为元组 。 让我们看看如何编写一个函数以从可变数量的参数返回元组。
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}')
Output:
输出:
Python Function Return Tuple
Python函数返回元组
Further Reading: Python *args and **kwargs
扩展阅读 : Python * args和** kwargs
Python函数返回另一个函数 (Python function returning another function)We can return a function also from the return statement. This is similar to Currying, which is the technique of translating the evaluation of a function that takes multiple arguments into evaluating a sequence of functions, each with a single argument.
我们也可以从return语句返回一个函数。 这类似于Currying ,这是一种将对带有多个参数的函数的求值转换为对函数序列进行求值的技术,每个函数都有一个参数。
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}')
Output:
输出:
Python Return Function
Python返回函数
Python函数返回外部函数 (Python function returning outer function)We can also return a function that is defined outside of the function with return statement.
我们还可以使用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}')
Output:
输出:
Python Function Return Outer Function
Python函数返回外部函数
Python返回多个值 (Python return multiple values)If you want to return multiple values from a function, you can return tuple, list, or dictionary object as per your requirement.
如果要从一个函数返回多个值,则可以根据需要返回元组, 列表或字典对象。
However, if you have to return a huge number of values then using sequence is too much resource hogging operation. We can use yield, in this case, to return multiple values one by one.
但是,如果必须返回大量值,那么使用序列会占用太多资源。 在这种情况下,我们可以使用yield来一次返回多个值。
def multiply_by_five(*args):
for arg in args:
yield arg * 5
a = multiply_by_five(4, 5, 6, 8)
print(a)
# showing the values
for i in a:
print(i)
Output:
输出:
Python Return vs Yield
Python回报与收益
摘要 (Summary)The python return statement is used to return the output from a function. We learned that we can also return a function from another function. Also, expressions are evaluated and then the result is returned from the function.
python return语句用于返回函数的输出。 我们了解到,我们还可以从另一个函数返回一个函数。 同样,对表达式求值,然后从函数返回结果。
GitHub Repository. GitHub存储库中检出完整的python脚本和更多Python示例。翻译自: https://www.journaldev.com/26183/python-return-statement



