python generators and iterators

#! /usr/bin/env python 
# -*- coding:utf-8 -*-

def xrange(n):
num = 0
while True:
if num > n:
return
print(num)
yield num
num += 1

obj = xrange(5)# obj is that the generator only has the ability to generate #Iterator
: with access ability, no random access, only from the beginning to the end
# Can't go back, which is convenient for looping relatively large data sets
# n1 = obj.__next__()
# n2 = obj.__next__()
# n3 = obj.__next__()

for n in xrange(10):
print(n)












































Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324734158&siteId=291194637