Python yield的简单应用和理解(代码)
程序员文章站
2022-05-03 19:33:36
Python yield的简单应用和理解(代码)
# coding=utf-8
from random import randint
def rand_gen(a...
Python yield的简单应用和理解(代码)
# coding=utf-8 from random import randint def rand_gen(aList): while len(aList) > 0: yield aList.pop(randint(0, len(aList)-1)) def counter(start_at=0): count = start_at while True: # 第一次val等于yield的返回值,随后因为while的存在yield没有返回值,yield返回None # 随后count被加一, yield再次有值可以返回 val = (yield count) if val is not None: count = val else: count += 1 for item in rand_gen(['rock', 'paper', 'scissors']): print item count = counter(5) print count.next() # 5 print count.next() # 6 print count.send(9) # 9 print count.next() # 10 print count.close() # None
推荐阅读
-
python中subprocess包的理解和典型应用
-
PHP call_user_func和call_user_func_array函数的简单理解与应用分析
-
PHP call_user_func和call_user_func_array函数的简单理解与应用分析
-
Python yield的简单应用和理解(代码)
-
SQL简单的存储过程理解和应用
-
简单且有用的Python数据分析和机器学习代码
-
理解神经网络,从简单的例子开始(1)7行python代码构建神经网络
-
python orm框架SQLAlchemy简单应用(数据库操作)的实例代码
-
Python3中类Class和对象object的理解(代码示例)
-
Python yield的简单应用和理解(代码)