函数对象
day21
def foo(): print("foo") print(foo) # 打印出对象地址 # 函数可以被赋值 f = foo print(f) # 打印出对象地址,结果和print(foo)一样 f() # 也可直接调用执行 # 把函数当成参数传递 def bar(func): print(func) func() bar(foo) # 把函数当成返回值 def bar2(func): print(func) return func g = bar2(foo) print(g) g() # 把函数当做容器类型的元素去用 def add(): print("-------> function add") def delete(): print("--------> function delete") def change(): print("--------> funciton change") def search(): print("---------> function search") def tell_msg(): msg = ''' add:增加 delete:删除 change:修改 search:查找 ''' print(msg) cmd_dic = { "add": add, "delete": delete, "change": change, "search": search } while True: tell_msg() choice = input("please inout your choice:").strip() # print(cmd_dic[choice]) #打印出的是内存地址 cmd_dic[choice]() # 直接执行
共有 0 条评论