31.Python:函数对象


# 精髓:可以把函数当成变量去用
# func=内存地址
# def func():
# print('from func')


# 1.可以赋值
# f = func
# print(f, func)


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

# 3.可以把函数当做另外一个函数的返回值
# def foo(x):
# return x
#
#
# res = foo(func)
# print(res)
#
# res()
# foo(func())

# 4.可以当作容器类型的一个元素
# l1 = [func, ]
# print(l1)
# l1[0]()
#
# dic = {'k1': func}
# print(dic)
# dic['k1']()

# 银行系统小程序练习

def login():
print('登录')


def transfer():
print('转账')


def check_balance():
print('查询余额')


def withdraw():
print('提现')


func_dic = {
'1': login,
'2': transfer,
'3': check_balance,
'4': withdraw
}

while True:
print("""
0 退出
1 登录
2 转账
3 查询余额
4 提现
""")
choice = input('业务编号:').strip()
if not choice.isdigit():
print("请输入数字!")

if choice == '0':
print('业务处理完了')
break

if choice in func_dic:
func_dic[choice]()
else:
print('输入的指令不存在')
posted @ 2021-06-25 14:01  SEPIA  阅读(70)  评论(0)    收藏  举报