for iteration

Discussion in 'Python' started by Franck Bui-Huu, Aug 22, 2003.

  1. 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
    Franck Bui-Huu, Aug 22, 2003
    #1
    1. Advertising

  2. Franck Bui-Huu

    Peter Otten Guest

    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
    Peter Otten, Aug 22, 2003
    #2
    1. Advertising

  3. 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 # 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
    >
    >
    Franck Bui-Huu, Aug 25, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dwerdna
    Replies:
    5
    Views:
    5,321
    Ajeetha
    Mar 31, 2005
  2. Bruce Whitehouse
    Replies:
    3
    Views:
    386
    Alvin Bruney
    Nov 23, 2003
  3. jurujuba
    Replies:
    3
    Views:
    1,540
    Eliyahu Goldin
    Feb 20, 2005
  4. =?Utf-8?B?TmlnZ3k=?=

    Dataset Iteration

    =?Utf-8?B?TmlnZ3k=?=, Jun 10, 2005, in forum: ASP .Net
    Replies:
    0
    Views:
    415
    =?Utf-8?B?TmlnZ3k=?=
    Jun 10, 2005
  5. Rudi
    Replies:
    5
    Views:
    4,969
Loading...

Share This Page