Python return函数返回值类型和帮助函数使用教程

作者:村长 时间:2021-01-13 14:21:32 

引言

经过函数学习之后我们会发现函数不被调用是不会直接执行的,我们在之前的函数调用之后发现运行的结果都是函数体内print()打印出来的结果,但是有时候为了方便函数参与二次运算,我们让函数体内不输出任何结果,而是把函数本身就当做一种结果,输出这种结果的方式就可以理解为返回函数的结果,python用return关键词来返回。下面我们对比几种不同的函数调用结果。

一、函数的输出方式对比

1.直接使用print打印函数运行结果:直接调用函数名传参即可。

def func1(a, b):
   res = a + b
   print(res)
func1(4, 9)
返回结果:13

2.打印没有返回值,没有输出代码块的函数,需要把函数当做一个变量来用print输出。

def func2(a, b):
   res = a + b
print(func2(4, 9))
返回结果:None

3.打印有返回值(return)的函数,同上,也是把函数当做一个变量来输出。

def func3(a, b):
   res = a + b
   return res
   # print(a)  # return后面的代码不会被执行
print(func3(4, 9))
返回结果:13

对比上面三种形式的函数,如果我们想用函数的结果来做运算的话,第一种情况就无法实现,比如

func1(4, 9) * 3
返回结果:
TypeError: unsupported operand type(s) for *: 'NoneType' and 'int'

 第二种情况本身就是None,所以忽略,第三种情况我们再试试

print(func3(4, 9) * 3)
返回结果:39

从上面的结果可以看出,有返回值的函数用起来很方便,直接可以当做变量来使用。

二、return的作用

同时return还有结束函数代码块的功能,return之后的下一行语句不会被执行。
注意:有返回值的函数一般直接调用函数名是不执行任何结果的,赋值给变量后才会返回结果。如果一个函数没有return语句,其实它有一个隐含的语句,返回值是None,类型也是'None Type'。print是打印在控制台,而return则是将后面的部分作为返回值。”
下面再来看看return的一些特别之处。

1.可以return多个结果

def func3(a, b):
   res1 = a + b
   res2 = a - b
return res1, res2
print(func3(4, 9))
返回结果:13  -5

2.一个函数可以有多个return,但是只会执行第一个

def func3(a, b):
   res1 = a + b
   res2 = a - b
   return res1
return res2
print(func3(4, 9))
返回结果:13

3.没有return的函数返回NoneType

def func3(a, b):
   res1 = a + b
res2 = a - b
print(type(func2(4, 9)))
返回结果:<class 'NoneType'>

三、帮助函数

这里属于一个补充知识点,我们在函数使用的时候不知道传参和函数的其他用法的时候可以使用help()函数来输出开发文档中的文本提示。

help(print)import os  #文件目录操作模块
os.mkdir('123')
help(os.mkdir)

返回结果:

Help on built-in function print in module builtins:

print(...)
    print(value, ..., sep=' ', end='\n', file=sys.stdout, flush=False)
    
    Prints the values to a stream, or to sys.stdout by default.
    Optional keyword arguments:
    file:  a file-like object (stream); defaults to the current sys.stdout.
    sep:   string inserted between values, default a space.
    end:   string appended after the last value, default a newline.
    flush: whether to forcibly flush the stream.
Help on built-in function mkdir in module nt:
mkdir(path, mode=511, *, dir_fd=None)
    Create a directory.    

    If dir_fd is not None, it should be a file descriptor open to a directory,
      and path should be relative; path will then be relative to that directory.
    dir_fd may not be implemented on your platform.
      If it is unavailable, using it will raise a NotImplementedError.

    The mode argument is ignored on Windows.

 以上是关于Python函数返回值类型和帮助函数的讲解,更多关于Python return帮助函数的资料请关注脚本之家其它相关文章!

来源:http://www.wakey.com.cn/document-func-return.html

标签:Python,return,返回值类型,帮助函数
0
投稿

猜你喜欢

  • DreamweaverMX 2004打造细线表格

    2008-10-01 09:39:00
  • html注释所引起的一系列问题

    2008-11-04 13:23:00
  • MySQL中两种快速创建空表的方式的区别

    2008-12-17 14:34:00
  • 如何才能有效对抗MySQL数据库的解密高手

    2009-05-22 18:21:00
  • thinkphp微信开发(消息加密解密)

    2023-11-21 06:08:43
  • Dreamweaver使用快技法十三则

    2009-07-21 12:45:00
  • 将MySQL 5.0下的数据导入到MySQL 3.23中

    2009-01-04 13:02:00
  • SpringBoot 中使用JSP的方法示例

    2023-06-16 22:35:09
  • ASP下批量删除数据的两种方法

    2011-02-05 11:01:00
  • 微软建议的ASP性能优化28条守则(7)

    2005-05-30 16:02:00
  • Golang算法之田忌赛马问题实现方法分析

    2023-06-29 06:07:24
  • 利用types增强vscode中js代码提示功能详解

    2023-07-15 05:57:08
  • 数据库连接字符串的常见问题和解决方法

    2008-11-28 15:16:00
  • 技术性击倒与抬杠

    2009-02-12 12:28:00
  • linux下安装apache与php;Apache+PHP+MySQL配置攻略

    2023-11-14 15:43:46
  • 设计较好付款流程的12个建议

    2009-06-08 12:45:00
  • Javascript: 为<input>设置readOnly属性问题,希望大家以后要小心

    2009-07-23 20:24:00
  • WEB2.0网页制作标准教程(9)第一个CSS布局实例

    2008-02-19 19:05:00
  • asp如何做一个看他爱不爱你的小测验?

    2010-07-11 21:16:00
  • Oracle11.2 命令行手工最简创建数据库的过程

    2009-09-14 12:07:00
  • asp之家 网络编程 m.aspxhome.com