--迭代器
class SkillIterator:
def __init__(self,data):
self.__data=data
self.__index = -1
def __next__(self):
if self.__index < len(self.__data)-1:
raise StopIteration()
return self.__data[self.__index]
class SkillManager:
def __init__(self):
self.skills = []
def add_skills(self,skill):
self.__skills.append(skill)
def __iter__(self):
return SkillIterator(self.__skills)
dict01 = {"唐僧":23,'悟空':29,'八戒':60}
iterator = dict01.__iter__()
while True:
try:
item = iterator.__next__()
print(item,dict01[item])
except StopIteration:
break
|