Class: __getitem__ demo : Class getitem « Class « Python






Class: __getitem__ demo

Class: __getitem__ demo

class indexer:
     def __getitem__(self, index):
         return index ** 2

X = indexer()
print X[2]                        # X[i] calls __getitem__(X, i)

for i in range(5): 
     print X[i],             

           
       








Related examples in the same category

1.How to call __getitem__How to call __getitem__