zoukankan      html  css  js  c++  java
  • Python 函数返回值

    本章详细介绍 返回值

    0x 00 返回值简介

    0x 01 指定返回值与隐含返回值

    0x 02 return 语句位置与多条 return 语句

    0x 03 返回值类型

    0x 04 函数嵌套

    0x 00 返回值简介

    • 回顾下,上一节简单介绍了函数及其各种参数,其中也有简单介绍 print 和 return 的区别,print 仅仅是打印在控制台,而 return 则是将 return 后面的部分作为返回值作为函数的输出,可以用变量接走,继续使用该返回值做其它事。
    • 函数需要先定义后调用,函数体中 return 语句的结果就是返回值。如果一个函数没有 reutrn 语句,其实它有一个隐含的 return 语句,返回值是 None,类型也是 'NoneType'。
    • return 语句的作用:
    • 结束函数调用、返回值

    0x 01 指定返回值与隐含返回值

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

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

    指定 return 返回值函数举例:

      

    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
    None
    <class 'NoneType'>
    

      

     

    0x 02 return 语句位置与多条 return 语句

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

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

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

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

    • 如果函数执行了 return 语句,函数会立刻返回,结束调用,return 之后的其它语句都不会被执行了

    举例 1:

     1 def showplus(x):
     2     print(x)
     3     return x + 1
     4     print(x + 1)  #该语句会执行么
     5 
     6 print(showplus(6))
     7 
     8 
     9 输出结果:
    10 6
    11 7
    View Code

    举例 2:

     1 def showplus(x):
     2     print(x)       # 5
     3     return x + 1   # 6
     4     return x + 2   # 该语句也不会被执行
     5 
     6 print(showplus(5))
     7 
     8 
     9 输出结果:
    10 5
    11 6
    View Code

    举例 3:

     1 def guess(x):
     2     if x > 3:
     3         return "> 3"
     4     else:
     5         return "<= 3"
     6 
     7 print(guess(10))
     8 print(guess(2))
     9 
    10 
    11 输出结果:
    12 > 3
    13 <= 3
    View Code

    举例 4:

    # for .. else .. 语句 (意外终止情况)

    # 表示如果 for 语句段的内容正常循环结果才会执行 else 段的语句,如果 for 在循环过程中时被 break 或者 return 语句意外终止循环,就不会执行 else 段中的语句。

    def fn(x):
        for i in range(x):
            if i > 4:
                return i
        else:
            print("{} is not greater than 4".format(x))
    
    print(fn(3))
    print(fn(6))
    
    
    返回结果:
    3 is not greater than 4
    None
    5
    View Code

    0x 03 返回值类型

    • 无论定义的是返回什么类型,return 只能返回单值,但值可以存在多个元素。

    • return [1,3,5] 是指返回一个列表,是一个列表对象,1,3,5 分别是这个列表的元素

    • return 1,3,5 看似返回多个值,隐式地被Python封装成了一个元祖返回

    举例 1:

    def fn():
        return 3   #单值时,返回的是什么类型
        
    print(fn())
    print(type(fn()))
    
    
    输出结果:
    3
    <class 'int'>    #int 整数类型
    View Code



    举例 2:

    def showlist():
        return [1,3,5]   #多元素,返回的是什么类型
    
    print(type(showlist()))
    print(showlist())
    
    
    输出结果:
    <class 'list'>
    [1, 3, 5]    #列表类型
    View Code

      

    举例 3:

    def showlist():
        return (2,4,6)   #多元素,返回的是什么类型
    
    print(type(showlist()))
    print(showlist())
    
    
    输出结果:
    <class 'tuple'>    #元祖类型
    (2, 4, 6)
    View Code

    举例 4:

    def showlist():
        return 2,4,6   #多值时,不指定类型
    
    print(type(showlist()))
    print(showlist())
    
    
    输出结果:
    <class 'tuple'>    #默认封装成元祖类型
    View Code

    0x 04 函数嵌套

    • 函数有可见范围(内外可见关系),这就是作用域的概念。

    • 内部函数不能被外部直接调用,会抛异常 NameError。

    举例 1:

    def outer():
        def inner():  #可以理解为内部函数
            print("inner")  
        print("outer")
    outer()
    
    
    输出结果:
    outer
    View Code

    此时如果调用 outer(),只会执行 print("outer"),因为 inner 虽然在 outer 函数内,但它也是一个函数,函数如果要调用,就必须用 '函数名()' 方式。

    举例 2:

    def outer():
        def inner():
            print("inner")
        print("outer")
    
    inner()   #外部无法引用内部函数,内部函数只在本地作用域有效
    
    
    输出结果,抛出异常:
    Traceback (most recent call last):
      File "C:/python/return_value.py", line 6, in <module>
        inner()
    NameError: name 'inner' is not defined
    View Code

    举例 3:

    def outer():
        def inner():
            print("inner")
        print("outer")
        inner()
    
    outer()
    
    输出结果:
    outer
    inner
    View Code

    总结:

    此节介绍了函数的返回值、返回值的作用,指定与不指定返回值时的不同,返回值类型,以及函数嵌套时返回值的使用。

  • 相关阅读:
    docker 学习
    grpc 学习
    ubuntu 完全干净的卸载docker
    numpy学习
    2020年假期sql excel文件 获取
    (a2b_hex)binascii.Error: Non-hexadecimal digit found
    数据库索引学习
    网络基础之网络协议
    Day11 进程相关
    基于socket套接字的网络通讯
  • 原文地址:https://www.cnblogs.com/i-honey/p/7679897.html
Copyright © 2011-2022 走看看