for iteration

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Franck Bui-Huu

    for iteration

    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


  • Peter Otten

    #2
    Re: for iteration

    Franck Bui-Huu wrote:
    [color=blue]
    > 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[/color]

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

    class MyList(list):
    def __getitem__(sel f, 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(myLis t)):
    print myList[index]

    print

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

    Peter

    Comment

    • Franck Bui-Huu

      #3
      Re: for iteration

      Thanks for your example but I haven't found __iter__ method in list type:
      [color=blue][color=green][color=darkred]
      >>> dir(list)[/color][/color][/color]
      ['__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'][color=blue][color=green][color=darkred]
      >>>[/color][/color][/color]

      Do you know why ?

      Peter Otten a écrit:
      [color=blue]
      >Franck Bui-Huu wrote:
      >
      >
      >[color=green]
      >>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
      >>
      >>[/color]
      >
      >For the for loop to work properly, you have to overide the __iter__()
      >method, e.g.:
      >
      >class MyList(list):
      > def __getitem__(sel f, 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(myLis t)):
      > print myList[index]
      >
      >print
      >
      ># works with __iter__()
      >for item in myList:
      > print item
      >
      >Peter
      >
      >[/color]


      Comment

      Working...