for iteration

F

Franck Bui-Huu

Hello,

I'm trying to customize a list by overriding __getitem__ method but
this change seems to not work with for iteration.
When I use my customized list in a for iteration, all changes made
in __getitem__ are not take into account.
How can I modify this behaviour ?

Thanks
 
P

Peter Otten

Franck said:
Hello,

I'm trying to customize a list by overriding __getitem__ method but
this change seems to not work with for iteration.
When I use my customized list in a for iteration, all changes made
in __getitem__ are not take into account.
How can I modify this behaviour ?

Thanks

For the for loop to work properly, you have to overide the __iter__()
method, e.g.:

class MyList(list):
def __getitem__(self, index):
""" for the sake of this example convert item to uppercase """
return list.__getitem__(self, index).upper()
def __iter__(self):
""" implemented using a generator """
for i in range(len(self)):
yield self # calls the customized __getitem__()

myList = MyList(["alpha", "beta", "gamma"])

# works with __getitem__()
for index in range(len(myList)):
print myList[index]

print

# works with __iter__()
for item in myList:
print item

Peter
 
F

Franck Bui-Huu

Thanks for your example but I haven't found __iter__ method in list type:
['__add__', '__class__', '__contains__', '__delattr__', '__delitem__',
'__delsli
ce__', '__doc__', '__eq__', '__ge__', '__getattribute__', '__getitem__',
'__gets
lice__', '__gt__', '__hash__', '__iadd__', '__imul__', '__init__',
'__le__', '__
len__', '__lt__', '__mul__', '__ne__', '__new__', '__reduce__',
'__repr__', '__r
mul__', '__setattr__', '__setitem__', '__setslice__', '__str__',
'append', 'coun
t', 'extend', 'index', 'insert', 'pop', 'remove', 'reverse', 'sort']
Do you know why ?

Peter Otten a écrit:
Franck Bui-Huu wrote:


Hello,

I'm trying to customize a list by overriding __getitem__ method but
this change seems to not work with for iteration.
When I use my customized list in a for iteration, all changes made
in __getitem__ are not take into account.
How can I modify this behaviour ?

Thanks

For the for loop to work properly, you have to overide the __iter__()
method, e.g.:

class MyList(list):
def __getitem__(self, index):
""" for the sake of this example convert item to uppercase """
return list.__getitem__(self, index).upper()
def __iter__(self):
""" implemented using a generator """
for i in range(len(self)):
yield self # calls the customized __getitem__()

myList = MyList(["alpha", "beta", "gamma"])

# works with __getitem__()
for index in range(len(myList)):
print myList[index]

print

# works with __iter__()
for item in myList:
print item

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top