Python之函数对象

函数对象:

# 函数对象指的是函数可以被当成变量去使用
def foo():  # foo = 函数的内存地址
    print('from foo')

1、可以被赋值:

f = foo
print(f is foo)
f()

2、以当作参数传给一个函数:

def bar(func):
    print(func)
    func()

bar(foo)

3、可以当成一个函数的返回值:

def bar(func):
    return func

res=bar(foo)
print(res)

4、可以当成容器类型的元素:

l = [foo]

print(l)
l[0]()

5、示例:

def login():
    print('登录功能......')


def withdraw():
    print('提现功能......')


def transfer():
    print('转账功能......')

def recharge():
    print('充值功能')

func_dic={
    "1": [login,"登录"],
    "2": [withdraw,"提现"],
    "3": [transfer,"转账"],
    "4": [recharge,"充值"]
}

# func_dic["1"][0]()


while True:
    print("0    退出")
    for k in func_dic:
        print("%s    %s" %(k,func_dic[k][1]))

    choice = input("Cmd: ").strip()
    if choice == "0":
        break
    if choice in func_dic:
        func_dic[choice][0]()
    else:
        print('输入的指令不存在')
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值