码迷,mamicode.com
首页 > 其他好文 > 详细

函数对象

时间:2018-07-18 23:35:07      阅读:150      评论:0      收藏:0      [点我收藏+]

标签:not   fun   input   draw   bar   bre   get   func   退出   

函数对象

#1-函数可以的作为数据被引用

def foo():
print("from foo")
foo()

print(foo)
func = foo
print(func)
func()

#2-可以当作一个函数传给另外一个函数

def foo():
print("from foo")
foo()

def bar(x):
print(x)
x() # x() = foo()
bar(foo) #foo 作为实参传给了x

#3-可以当作函数的返回值

def foo():
print("from foo")

def bar():
return foo
f=bar()
print(f is foo )
f()

#4-可以当作容器类元素
def f1():
print("from f1")

def f2():
print("from f2")

def f3():
print("from f3")

l=[f1,f2,f3]
print(l)
l[0]()

#5-购物场景
def auth():
print("please login")

def shopping():
print("chose what you want")

def withdraw():
print("get your money")

def leave():
print("please logout")

func_dic={
‘1‘: auth,
‘2‘: shopping,
‘3‘: withdraw,
‘4‘: leave
}
while True:
print("""
1:登录
2:购物
3:转账
4:退出
""")

choice = input(‘请输入您要执行的操作:‘).strip() # choice=‘123‘
if choice == ‘0‘: break
if choice not in func_dic:
print(‘输入错误的指令,请重新输入‘)
continue

func_dic[choice]()

 

---恢复内容结束---

---恢复内容结束---

函数对象

标签:not   fun   input   draw   bar   bre   get   func   退出   

原文地址:https://www.cnblogs.com/qinmao/p/9332723.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!