欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 健康 > 美食 > python函数返回值是什么

python函数返回值是什么

2025/2/1 17:41:36 来源:https://blog.csdn.net/hakesashou/article/details/143024408  浏览:    关键词:python函数返回值是什么

函数返回值简介

1、简单介绍print和return的区别,print仅仅是打印在控制台,而return则是将return后面的部分作为返回值:作为函数的输出,可以用变量接走,继续使用该返回值做其它事。

2、函数需要先定义后调用,函数体中return语句的结果就是返回值。如果一个函数没有reutrn语句,其实它有一个隐含的return语句,返回值是None,类型也是'NoneType'。

def func(x,y):num = x + yreturnprint(func(1,2))#上面代码的输出结果为:None

从上面例子可以看出print( )只是起一个打印作用,函数具体返回什么由return决定。

return语句的作用:

结束函数调用、返回值

指定返回值与隐含返回值:

1、函数体中return语句有指定返回值时返回的就是其值。

2、函数体中没有return语句时,函数运行结束会隐含返回一个None作为返回值,类型是NoneType,与return 、return None 等效,都是返回 None。

def showplus(x):
print(x)
return x + 1
num = showplus(6)
add = num + 2
print(add)
#上面函数的输出结果为:6、9

隐含return None 举例:

def showplus(x):
print(x)
num = showplus(6)
print(num)
print(type(num))
"""
上面函数的输出结果为:6
6
None
<class 'NoneType'>
"""

函数返回值赋值给变量:

import os
import sys
import subprocessdef get_manifest_xml_path():xml_path = input()if os.path.exists( xml_path ):return xml_pathelse:print('AndroidManifest.xml not found!')  def get_out_path( xml_path ):return os.path.dirname( os.path.abspath( xml_path ) ) + os.sep + 'AndroidManifest.txt'  def convert_xml_to_txt( xml_path, out_path ):convert_cmd = 'java -jar AXMLPrinter2.jar %s>%s' % ( xml_path, out_path )subprocess.Popen( convert_cmd, shell=True )if __name__ == "__main__":xml_path = get_manifest_xml_path()out_path = get_out_path( xml_path )convert_xml_to_txt( xml_path, out_path )

return 语句位置与多条 return 语句

1、python函数使用return语句返回 "返回值",可以将其赋给其它变量作其它的用处;

2、所有函数都有返回值,如果没有return语句,会隐式地调用 return None 作为返回值;

3、一个函数可以存在多条return语句,但只有一条可以被执行,如果没有一条reutrn语句被执行,同样会隐式调用return None作为返回值;

4、如果有必要,可以显式调用return None明确返回一个None(空值对象)作为返回值,可以简写为return,不过python中懒惰即美德,所以一般能不写就不写;

5、如果函数执行了return语句,函数会立刻返回,结束调用,return之后的其它语句都不会被执行了(可用于结束代码块)。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com