python函數實例應用案例


一、函數的用法

1. 函數可以做賦值

def func():
    print('from func')
f=func()

2.函數可以當做參數被調用

3.可以放入列表等當中

1 l=[func,]     #作為元素時,函數名不加括號
2 l[0]()       #調用
View Code

4.案例1:

  1)小白寫的程序 

 1 def login():
 2     print('登錄功能')
 3 def transfer():
 4     print('轉賬功能')
 5 def select():
 6     print('查詢余額')
 7 while True:
 8     print(''''
 9     0 退出
10     1 登錄
11     2 轉賬
12     3 查詢余額
13     ''')
14     chice=input('請輸入命令編號:').strip()
15     if not chice.isdigit():
16         continue
17     if chice=='0':
18         break
19     elif chice=='1':
20         login()
21     elif chice=='2':
22         transfer()
23     elif chice == '3':
24         select()
25     else:
26         print('輸入指令不存在')
View Code

  2)我進一步優化(程序寫死后添加功能不方便,一個功能寫一個函數)

 

 1 def login():
 2     print('登錄功能')
 3 def transfer():
 4     print('轉賬功能')
 5 def select():
 6     print('查詢余額')
 7 func_dict={
 8     '1':login,
 9     '2':transfer,
10     '3':select
11 }
12 while True:
13     print(''''
14     0 退出
15     1 登錄
16     2 轉賬
17     3 查詢余額
18     ''')
19     chice=input('請輸入命令編號:').strip()
20     # if not chice.isdigit():
21     #     continue
22     # if chice=='0':
23     #     break
24     # elif chice=='1':
25     #     login()
26     # elif chice=='2':
27     #     transfer()
28     # elif chice == '3':
29     #     select()
30     # else:
31     #     print('輸入指令不存在')
32     if chice in func_dict.keys():
33         func_dict[chice]()
34     else:
35         print('輸入指令不存在')
View Code

  3)高手的優化

 1 def login():
 2     print('登錄功能')
 3 def transfer():
 4     print('轉賬功能')
 5 def select():
 6     print('查詢余額')
 7 func_dict={
 8     '0':['退出',None],
 9     '1':['登錄',login],
10     '2':['轉賬',transfer],
11     '3':['查詢余額',select]
12 }
13 while True:
14     # print(''''
15     # 0 退出
16     # 1 登錄
17     # 2 轉賬
18     # 3 查詢余額
19     # ''')
20     for k in func_dict:      #解放以上print內容
21         print(k,func_dict[k][0])
22     chice=input('請輸入命令編號:').strip()
23     # if not chice.isdigit():
24     #     continue
25     if chice=='0':
26         break
27     # elif chice=='1':
28     #     login()
29     # elif chice=='2':
30     #     transfer()
31     # elif chice == '3':
32     #     select()
33     # else:
34     #     print('輸入指令不存在')
35     if chice in func_dict.keys():
36         func_dict[chice][1]()
37     else:
38         print('輸入指令不存在')
View Code

5.函數的嵌套調用及定義

 1 def bijiao():
 2 # 對4個數字比較大小
 3     def max2(x,y):
 4         if x>y:
 5             return x
 6         else:
 7             return y
 8     def max4(a,b,c,d):
 9         res1=max2(a,b)
10         res2=max2(res1,c)
11         res3=max2(res2,d)
12         return res3
View Code

優點:減少代碼量,清晰明了

 二、閉包函數

      閉包函數=名稱空間與作用域+函數嵌套+函數對象        (核心點:名字的查找關系是以函數定義階段為准)

  1.閉包的解釋

    ‘閉’指的是該函數在函數內部;

    “包”指的是該函數包含對外層函數作用域名字(外不函數的變量)的引用; 

  2.定義閉包函數

    內部函數可以實現在外部調用。

1 def f1():
2     x=22222
3     def f2():
4         print(x)
5     return f2         #不管函數怎么調用,f2調用的永遠是f1當中的變量,f2可以在任何地方調用
View Code

 


免責聲明!

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



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