python之函数用法__getitem__()
# -*- coding: utf-8 -*- #python 27 #xiaodeng #python之函数用法__getitem__() #http://www.cnblogs.com/hongfei/p/3858256.html #__getitem__ #如果类把某个属性定义为序列,可以使用__getitem__()输出序列属性中的某个元素. class FruitShop(): def __getitem__(self,i): return self.fruits[i]#可迭代对象 if __name__ == "__main__": shop = FruitShop() print shop #__main__.FruitShop instance shop.fruits = ["apple", "banana"] print shop[1] #banana for item in shop: print item
无语言基础,自学python所做的各种笔记,欢迎大牛指点.