热线电话:13121318867

登录
2019-07-09 阅读量: 460
python内置迭代器实例

# A simple Python program to demonstrate

# working of iterators using an example type

# that iterates from 10 to given value

# An iterable user defined type

class Test:

# Cosntructor

def __init__(self, limit):

self.limit = limit

# Called when iteration is initialized

def __iter__(self):

self.x = 10

return self

# To move to next element. In Python 3,

# we should replace next with __next__

def next(self):

# Store current value ofx

x = self.x

# Stop iteration if limit is reached

if x > self.limit:

raise StopIteration

# Else increment and return old value

self.x = x + 1;

return x

# Prints numbers from 10 to 15

for i in Test(15):

print(i)

# Prints nothing

for i in Test(5):

print(i)

0.0000
2
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子