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

 

 

 

總結:

 

此節介紹了函數的返回值、返回值的作用,指定與不指定返回值時的不同,返回值類型,以及函數嵌套時返回值的使用。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM