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)

猜你喜欢

转载自www.cnblogs.com/wanghzh/p/9175737.html