第三篇:函数之对象

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]() #

猜你喜欢

转载自www.cnblogs.com/yspass/p/9325678.html