迭代器
class MyRev:def __init__(self,seq):
self.m = len(seq)
self.seq = ''
while self.m != 0:
self.seq += seq
self.m -= 1
self.i = 0
def __iter__(self):
return self
def __next__(self):
while self.i == self.m:
raise StopIteration
self.i += 1
temp = self.seq
return temp
结果什么都不打印= = 心里苦QAQ
>>> myrev = MyRev('FishC')
>>> for i in myrev:
print(i,end='')
>>> 这样试试:
class MyRev:
def __init__(self,seq):
self.m = len(seq)
self.seq = ''
while self.m != 0:
self.seq += seq
self.m -= 1
self.i = 0
def __iter__(self):
return self
def __next__(self):
if self.i == self.m:
raise StopIteration
self.i += 1
temp = self.seq
return temp
zltzlt 发表于 2020-8-10 09:18
这样试试:
懂了 self.i在外面= =while直接结束了
页:
[1]