Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > for iteration

Reply
Thread Tools

for iteration

 
 
Franck Bui-Huu
Guest
Posts: n/a
 
      08-22-2003
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


 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      08-22-2003
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[i] # 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
 
Reply With Quote
 
 
 
 
Franck Bui-Huu
Guest
Posts: n/a
 
      08-25-2003
Thanks for your example but I haven't found __iter__ method in list type:

>>> dir(list)

['__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[i] # 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
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Struts - Problem with nested iteration or double iteration Rudi Java 5 10-01-2008 03:30 AM
Dataset Iteration =?Utf-8?B?TmlnZ3k=?= ASP .Net 0 06-10-2005 02:25 PM
Showing value of loop iteration in assert statement dwerdna VHDL 5 03-31-2005 05:23 PM
implementing iteration- and conditional-tags with ASP.NET jurujuba ASP .Net 3 02-20-2005 08:16 AM
looping and displaying progress with every iteration Bruce Whitehouse ASP .Net 3 11-23-2003 07:15 PM



Advertisments