import time,random
from collections import Iterable,Iterator
class Classmate(object):
	def __init__(self):
		self.names = list()
	
	def add(self,name):
		self.names.append(name)
	
	def __iter__(self):
		"""如果想要一个对象成为一个可以迭代的对象，即可以使用for，那么必须实现__iter__方法"""
		return ClassIterator(self)
class ClassIterator(object):
	def __init__(self,obj):
		self.obj=obj
		self.current_num = 0
	def __iter__(self):
		pass
	
	def __next__(self):
		#for i in random.randint(0,2):
		if self.current_num<len(self.obj.names):
			ret = self.obj.names[self.current_num]
			self.current_num += 1
			return ret
		#	return ran_name

classmate = Classmate()

classmate.add("老王")
classmate.add("王二")
classmate.add("张三")

#print("判断classmate是否是可以迭代的对象：",isinstance(classmate,Iterable))
#classmate_iterator = iter(classmate)
#print("判断classmate_iterator是否是迭代器：",isinstance(classmate_iterator,Iterator))

for name in classmate:
	print(name)
	time.sleep(1)
