Python记录7:函数3,函数对象

#函数对象指的是:函数的内存地址可以像变量值一样去使用,如何使用?
def func():
print('from func')
#1. 可以被引用
# f=func
# print(f)
# f()

#2. 可以当做参数传给另外一个函数
# def bar(x):
# # print(x)
# x()
# bar(func)

#3. 可以当做函数的返回值
# def foo():
# return func
# res=foo()
# print(res)

#4. 可以当做容器类型的元素
# l=[func,]
# print(l)
# l[0]()

# dic={'0':func}
# # print(dic)
# dic['0']()


def login():
print('login')

def register():
print('register')

def shoppping():
print('shopping')

def pay():
print('pay')

def withdraw():
print("withdraw")

def transfer():
print('transfer')

func_dic={
'1':login,
'2':register,
'3':shoppping,
'4':pay,
'5':withdraw,
'6':transfer
}

while True:
print("""
0: 退出
1: 登录
2: 注册
3: 购物
4: 支付
5: 提现
6: 转账
""")
choice=input('输入操作>>>: ').strip()
if choice == '0': break
if choice in func_dic:
func_dic[choice]()
else:
print('输入的指令不存在')

猜你喜欢

转载自www.cnblogs.com/1832921tongjieducn/p/10070024.html