#!/usr/bin/env python
#encoding: utf-8
def my_gen():
print("This is printed once") # run once
i = 0
while (i<5):
print(i, 'Before yield')
yield i
print(i, 'After yield')
i+=1
print('This is printed when the while loop ends.')
for m in my_gen():
print("Of course, this is printed multiple times")
This is printed once
0 Before yield
Of course, this is printed multiple times
0 After yield
1 Before yield
Of course, this is printed multiple times
1 After yield
2 Before yield
Of course, this is printed multiple times
2 After yield
3 Before yield
Of course, this is printed multiple times
3 After yield
4 Before yield
Of course, this is printed multiple times
4 After yield
This is printed when the while loop ends.
最終更新:2016年10月19日 07:05