python 进阶读书笔记2 -- python魔法函数
#!/usr/bin/env python
# -*- coding: utf-8 -*-
class student:
def __init__(self, name_list):
self.student_name_list = name_list
def __getitem__(self, item):
return self.student_name_list[item]
stu = student(['tom', 'bob', 'jane', ])
stu = stu[:2]
l = len(stu)
for student_name in stu:
'''
1.寻找实例的__iter__方法
2.调用__getitem__方法,直到抛出异常
'''
print(student_name)