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

Re: [python-iter] Resolution at Python 9

Expand Messages
  • gzeljko
    From: Ka-Ping Yee ... Is it for use as def f(): pass for x in iter(f,sentinel): ly-y rs-gzeljko
    Message 1 of 68 , Mar 11, 2001
    • 0 Attachment
      From: Ka-Ping Yee <ping@...>
      >
      > 7. A two-argument built-in function (provisionally, "iter()" will
      > do this when given two arguments) will produce an iterator that
      > repeatedly calls the callable object given as the first argument
      > until it returns the sentinel value given as the second argument.
      >

      Is it for use as

      def f():
      pass

      for x in iter(f,sentinel):

      ly-y'rs-gzeljko
    • Guido van Rossum
      ... This is already implemented: look at the Queue module: http://python.sourceforge.net/devel-docs/lib/module-Queue.html To push an item into the Queue, you
      Message 68 of 68 , Mar 16, 2001
      • 0 Attachment
        > I think the closest way to do it in Python is to use threads.
        > A thread-aware queue which is an iterator at one end and a file-like
        > sink at the other, implemented once for all uses. Called Chan in
        > Concurrent Haskell. Should be a nice abstraction. Doesn't require
        > more magic than is already there.
        >
        > Well, so there are three approaches, and the third does fit Python!

        This is already implemented: look at the Queue module:

        http://python.sourceforge.net/devel-docs/lib/module-Queue.html

        To push an item into the Queue, you use q.put(x); to pull the next
        item from it, use x = q.get(). If you want a terminator, all you need
        to do is agree on a sentinel value. If you want an iterator, you can
        use iter(q, sentinel).

        The disadvantage is that it's way slow compared to stackframe-based
        solutions, and that's why it's not commonly used as an alternative for
        push/pull interfaces except where threads are already on the mind of
        the programmer.

        --Guido van Rossum (home page: http://www.python.org/~guido/)
      Your message has been successfully submitted and would be delivered to recipients shortly.