第三篇:函数之对象
1. 函數是第一類對象,意味著函數可以當作數據去使用
1 def foo(): 2 print('from foo') 3 4 5 #1、可以被引用 6 # print(foo) 7 # func=foo 8 # print(func) 9 # func() 10 11 #2、可以當作參數傳給另外一個函數 12 # def bar(x): #x=foo的內存地址 13 # print(x) 14 # x() 15 # 16 # bar(foo) 17 18 #3、可以當作函數的返回值 19 # def bar(): 20 # return foo 21 # 22 # f=bar() 23 # # print(f is foo) 24 # f() 25 26 #4、可以當作容器類型的元素 27 # def f1(): 28 # print('from f1') 29 # 30 # def f2(): 31 # print('from f2') 32 # 33 # l=[f1,f2] 34 # print(l) 35 # l[1]()?實際case
1 def pay(): 2 print('pay function') 3 4 def withdraw(): 5 print('withdraw function') 6 7 def auth(): 8 print('auth function') 9 10 def shopping(): 11 print('shopping function') 12 13 def transfer(): 14 print('transfer function') 15 16 func_dic={ 17 '1':pay, 18 '2':withdraw, 19 '3':auth, 20 '4':shopping, 21 '5':transfer 22 } 23 24 # print(func_dic) 25 # func_dic['2']() 26 27 while True: 28 print(""" 29 0 退出 30 1 支付 31 2 取款 32 3 認證 33 4 購物 34 5 轉賬 35 """) 36 choice=input('請輸入您要執行的操作:').strip() #choice='123' 37 if choice == '0':break 38 if choice not in func_dic: 39 print('輸入錯誤的指令,請重新輸入') 40 continue 41 42 func_dic[choice]() #?
轉載于:https://www.cnblogs.com/yspass/p/9325678.html
總結
- 上一篇: MP实战系列(十一)之封装方法详解(续一
- 下一篇: 爬取京东网页评论(动态网页)