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

182Re: [python-iter] prefer StopIteration to IndexError

Expand Messages
  • qrczak@knm.org.pl
    Mar 14, 2001
    • 0 Attachment
      Tue, 13 Mar 2001 10:38:33 -0800 (PST), Ka-Ping Yee <ping@...> pisze:

      > Hmm. Imagine for a moment that we're dealing with "for x in seq: body"
      > where body is expressed as a function taking x. It seems we would then
      > have to implement the for loop and the iter() constructor this way:
      >
      > def for_loop(seq, body):
      > it = iter(seq)
      > try:
      > while 1:
      > value = it.next()
      > body(value)
      > except IndexError:
      > pass
      > except InnerIndexError, value:
      > raise IndexError, value

      No. You should not wrap body in the exception handler.

      def for_loop(seq, body):
      it = iter(seq)
      while 1:
      try:
      value = it.next()
      except IndexError:
      break
      body(value)

      I don't see the need of inventing a new exception. Using the same
      has the advantage that an iterator implemented in terms of indexing
      doesn't have to translate the exception, it just lets it fly.

      --
      __("< Marcin Kowalczyk * qrczak@... http://qrczak.ids.net.pl/
      \__/
      ^^ SYGNATURA ZAST─śPCZA
      QRCZAK
    • Show all 68 messages in this topic