python學習筆記(十一)之函數
阿新 • • 發佈:2017-10-31
last 函數返回 traceback keep disco show 全局變量 not 默認參數
View Code
牛刀小試:
定義一個無參函數
1 >>> def myFirstFunc(): 2 ... print("Hello python") 3 ... print("hello world") 4 ... print("hello my fist func") 5 ... 6 >>> myFirstFunc() 7 Hello python 8 hello world 9 hello my fist funcView Code
定義一個有參函數
1 >>> def mySecondFunc(name):View Code2 ... print("hello", name) 3 ... 4 >>> mySecondFunc(‘zhz‘) 5 hello zhz
函數之形參和實參
1 >>> def add(first, second): 2 ... return first + second 3 ... 4 >>> add(1, 5) 5 6View Code
定義函數時,first和second就是形參,在函數調用時,傳遞的1和5就是實參。
函數之註釋和文檔
1 >>> def add(first,second):View Code2 ... ‘這是函數文檔:計算兩個參數的和‘ 3 ... #這是函數註釋:計算兩個參數的和 4 ... return first + second 5 ...
函數文檔可以使用以下方式查看
1 >>> add.__doc__ 2 ‘這是函數文檔:計算兩個參數的和‘ 3 4 >>> help(add) 5 6 Help on function add in module __main__: 7 8 add(first, second) 9 這是函數文檔:計算兩個參數的和
函數之關鍵字參數
1 >>> def saysome(name, words): 2 ... print(name, ‘->‘, words) 3 ... 4 >>> saysome(‘Jobs‘, ‘stay hungry,stay foolish‘) 5 Jobs -> stay hungry,stay foolish 6 >>> saysome(‘stay hungry,stay foolish‘,‘Jobs‘) 7 stay hungry,stay foolish -> Jobs 8 >>> saysome(words = ‘stay hungry,stay foolish‘,name = ‘Jobs‘) 9 Jobs -> stay hungry,stay foolishView Code
函數之默認參數
1 >>> def saysome(name = ‘Jobs‘, words = ‘stay hungry, stay foolish‘): 2 ... print(name, ‘->‘, words) 3 ... 4 >>> saysome() 5 Jobs -> stay hungry, stay foolish 6 >>> saysome(‘nazi‘) 7 nazi -> stay hungry, stay foolish 8 >>> saysome(words = ‘keep working‘) 9 Jobs -> keep working 10 >>> saysome(‘nazi‘,‘keep looking‘) 11 nazi -> keep lookingView Code
函數之收集參數
1 >>> def test(*params): 2 ... for i in range(len(params)): 3 ... print(params[i]) 4 ... 5 >>> test(1, ‘hello‘, (1,3, [‘abc‘])) 6 1 7 hello 8 (1, 3, [‘abc‘])View Code
收集參數後最好使用默認參數,用關鍵字參數調用。
函數返回值
python中,用return語句可以從函數返回一個對象,列表或元組。當沒有顯示調用return語句時,python會自動返回一個NoneType對象。所以,可以說python中只有函數,沒有過程。
1 >>> def hello(): 2 ... print("Hello") 3 ... 4 >>> temp = hello() 5 Hello 6 >>> print(temp) 7 None 8 >>> type(temp) 9 <class ‘NoneType‘> 10 >>> def back(): 11 ... return 1,2,‘abc‘,[1,2] 12 ... 13 >>> back() 14 (1, 2, ‘abc‘, [1, 2]) 15 >>> def back(): 16 ... return [1, 3.14, ‘abv‘, [2]] 17 ... 18 >>> back() 19 [1, 3.14, ‘abv‘, [2]]View Code
局部變量和全局變量
在函數內部聲明的變量是局部變量,在函數外聲明的變量是全局變量。
1 def discount(price,rate): 2 ‘final_price, price, rate are local variables‘ 3 final_price = price * rate 4 return final_price 5 6 if __name__ == ‘__main__‘: 7 ‘old_price, rate and new_price are global variables‘ 8 old_price = float(input("原價:")) 9 rate = float(input("折扣:")) 10 new_price = discount(old_price, rate) 11 print("折後價:", new_price)View Code
在函數中試圖修改一個全局變量的值時,會python創建一個和全局變量相同的局部變量,此時,修改的只是該局部變量,全局變量不變。
1 >>> number = 10 2 >>> def test(): 3 ... number = 5 4 ... 5 ... 6 >>> test() 7 >>> number 8 10View Code
要在函數內部修改全局變量的值,可以使用global關鍵字。
1 >>> number 2 10 3 >>> def test(): 4 ... global number 5 ... number = 5 6 ... 7 >>> test() 8 >>> number 9 5View Code
內嵌函數
在函數內部可以定義其他函數,這個內部函數的作用域僅限於外部函數內部。在外部函數外部的任何位置使用該內部函數,都會拋出一個異常。
1 >>> def funA(): 2 ... print("funA") 3 ... def funB(): 4 ... print("funB") 5 ... funB() 6 ... 7 >>> funA() 8 funA 9 funB 10 >>> funB() 11 Traceback (most recent call last): 12 File "<stdin>", line 1, in <module> 13 NameError: name ‘funB‘ is not definedView Code
閉包
如果在一個內部函數裏對外部作用域(非全局作用域)的變量進行引用,那麽內部函數被認為是一個閉包。
1 >>> def funX(x): 2 ... def funY(y): 3 ... return x*y 4 ... return funY 5 ... 6 >>> i = funX(5) 7 >>> type(i) 8 <class ‘function‘> 9 >>> i(6) 10 30 11 >>> funX(5)(6) 12 30View Code
同樣的,在閉包內修改外部作用域變量,系統會自動創建局部變量x,屏蔽外部變量。
1 >>> def fun1(): 2 ... x = 5 3 ... def fun2(): 4 ... x *= x 5 ... fun2() 6 ... 7 >>> fun1() 8 Traceback (most recent call last): 9 File "<stdin>", line 1, in <module> 10 File "<stdin>", line 5, in fun1 11 File "<stdin>", line 4, in fun2 12 UnboundLocalError: local variable ‘x‘ referenced before assignmentView Code
此時要修改外部變量,一,可以使用列表,列表不是放在棧裏。
1 >>> def fun1(): 2 ... x = [5] 3 ... def fun2(): 4 ... x[0] *= x[0] 5 ... fun2() 6 ... print(x) 7 ... 8 >>> fun1() 9 [25]View Code
二,可以使用nolocal關鍵字
1 def fun1(): 2 x = 5 3 def fun2(): 4 nonlocal x 5 x = 50 6 fun2() 7 print(x) 8 9 if __name__ == ‘__main__‘: 10 fun1()View Code
python學習筆記(十一)之函數