Loading ...
Sorry, an error occurred while loading the content.

190Thinking about next() and return self...

Expand Messages
  • Clark C. Evans
    Mar 16, 2001
      As I understand it, there are two options (the
      latter being implemented):

      a) Have an __iter__ to fetch a new iterator,
      and have __next__ to fetch the next value in
      the sequence.

      Advantages: Very clean built-ins, objects
      providing iterators have __iter__
      and iterators have __next__

      Disadvantages: Users can't directly use next()
      to fetch the next value, and
      it uses 2 slots

      b) Have an __iter__ to fetch a new iterator,
      and use next to fetch the next value.

      Advantages: Only one slot is used,
      and iter.next() works as

      Disadvantages: Objects already having a next()
      may not be possible to convert over
      to iterators (but a anonomous function
      will do, so this isn't a bigee).
      Iterators must return "self" in __iter__,
      which seems a bit hacky.

      I actually prefer #1, but since I don't know the ramifications
      of adding yet another slot to every object (32 bits?), option
      (b), the currently favored approach is perfectly fine.


      I have one question. Can __iter__ take arguments?
      In other words, can I support multiple iterator types
      via the iter() function?

      iter(obj,arg1,arg2) -> obj.__iter__(obj,arg1,arg2)

      Just wondering...
    • Show all 4 messages in this topic