python的学习之路(四)

#迭代器,取值只能用next方法,不能随意取值
name = iter([11,22,33,44])
print(name.__next__())
print(name.__next__())
print(name.__next__())

#生成器,函数含有yield则为生成器,生成器的返回值为迭代器
def cash_money(acount):
while acount>0:
acount -=100
yield 100
print('就是花钱,任性')

atm = cash_money(500)
print(atm.__next__())
print(atm.__next__())
print('出去消费')
print(atm.__next__())

import  time

def consumer(name):
print("%s 准备吃包子了!" % name)
while True:
baozi = yield
print("%s 包子来了,被 %s吃了!" % (baozi,name))


def producter(name):
print("开始做包子了")
c = consumer("A")
c1 = consumer("B")
c.__next__()
c1.__next__()
for i in range(10):
time.sleep(1)
print("做了两个包子")
c.send(i)
c1.send(i)
producter("alex")

#装饰器,函数前加@+函数名
def login(func):
def inner(args):
print("check now")
func(args)

return inner


def home(name):
print("welcome %s to home page" %name)

@login
def tv(name):
print("welcome %s to tv" %name)


def movie(name):
print("welcome to %s movie" %name)

#tv = login(tv)
# home = login(home)
# movie = login(movie)


tv("alex")
posted @ 2019-05-17 17:27  HarsonLee  阅读(212)  评论(0)    收藏  举报