迭代器生成器
Posted 有一天
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了迭代器生成器相关的知识,希望对你有一定的参考价值。
#!/usr/bin/python
#coding:utf-8
#print ‘Hello world‘
__metaclass__ = type
#class Fids:
# def __init__(self):
# self.a = 0
# self.b = 1
#
# def next(self):
# self.a, self.b = self.b, self.a + self.b
# return self.a
#
# def __iter__(self):
# return self
#
#fibs = Fids()
#for f in fibs:
# print f
# if f > 10:
# break
#print fibs.next()
def flatten(nested):
try:
try: nested + ‘‘
except TypeError:
print ‘except ‘
pass
else:
print ‘else ‘
raise TypeError
for sublist in nested:
for element in flatten(sublist):
yield(element)
except TypeError:
yield nested
print list(flatten([[[1], 2], ‘376‘, 4, [5, [6, 7]], 8]))
def yield_test(n):
for i in range(n):
yield call(i)
print ‘i=‘, i
print(‘do something.‘)
print(‘end.‘)
def call(i):
return i*2
for i in yield_test(5):
print i, ‘,‘
以上是关于迭代器生成器的主要内容,如果未能解决你的问题,请参考以下文章