mirror of
http://47.106.207.27:3000/Jeremy_liu/AI0814_jiaoan_public.git
synced 2026-09-27 06:14:14 +08:00
42 lines
819 B
Python
42 lines
819 B
Python
def my_generator():
|
|||
|
|
print("开始")
|
||
|
|
yield 1
|
||
|
|
print("继续")
|
||
|
|
yield 2
|
||
|
|
print("结束")
|
||
|
|
yield 3
|
||
|
|
|
||
|
|
# g = my_generator() # 注意:此时函数体不会执行!
|
||
|
|
# print(g,type(g)) # <class 'generator'>
|
||
|
|
# # print(print,type(print))
|
||
|
|
# print(next(g)) # 输出:开始 \n 1
|
||
|
|
# print(next(g)) # 输出:继续 \n 2
|
||
|
|
# print(next(g)) # 输出:结束 \n 3
|
||
|
|
# # print(next(g)) # 榨干了
|
||
|
|
|
||
|
|
# g2=my_generator()
|
||
|
|
# for i in g2:
|
||
|
|
# print(i)
|
||
|
|
|
||
|
|
# print(len(g2)) # 生成器没有len方法
|
||
|
|
|
||
|
|
|
||
|
|
# import time
|
||
|
|
# def slow_generator():
|
||
|
|
# for i in range(5):
|
||
|
|
# yield f"第 {i+1} 条数据\n"
|
||
|
|
# time.sleep(1) # 故意慢一点,方便观察
|
||
|
|
#
|
||
|
|
# g3=slow_generator()
|
||
|
|
# for i in g3:
|
||
|
|
# print(i)
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
import json
|
||
|
|
d=json.dumps('\n\n')
|
||
|
|
print(d)
|
||
|
|
# print('1\n\n2')
|