
Hello All, Currently `iter' 2'nd parameter is used for comparison. I'd like to have the ability to give a function that will determine weather to stop or not. iter(range(10), lambda x: x > 7) will stop at 8. (There reverse logic might be uses as well: continue as long a `sentinal' returns True.) The only problem I can see is that someone might want to use a function object as a sentinal, then `iter' won't know if to call the function or to compare. However this seems very rare and can be solved by: def f(x, y): .... iter(something, lambda x: x == f) Any thoughts/objections? Bye. -- ------------------------------------------------------------------------- Miki Tebeka <miki.tebeka@zoran.com> http://www.cs.bgu.ac.il/~tebeka The only difference between children and adults is the price of the toys.

On Feb 18, 2004, at 8:07 AM, Miki Tebeka wrote:
Currently `iter' 2'nd parameter is used for comparison. I'd like to have the ability to give a function that will determine weather to stop or not.
iter(range(10), lambda x: x > 7) will stop at 8. (There reverse logic might be uses as well: continue as long a `sentinal' returns True.)
The only problem I can see is that someone might want to use a function object as a sentinal, then `iter' won't know if to call the function or to compare. However this seems very rare and can be solved by: def f(x, y): .... iter(something, lambda x: x == f)
This is not necessary.
import itertools list(itertools.takewhile(lambda x: x < 8, range(10))) [0, 1, 2, 3, 4, 5, 6, 7]
(among the numerous other ways to say it with itertools .. or even generator comprehensions in Python 2.4) -bob

Miki Tebeka <miki.tebeka@zoran.com>:
Currently `iter' 2'nd parameter is used for comparison. I'd like to have the ability to give a function that will determine weather to stop or not.
I think there should be a different function for this, rather than overloading it onto iter() by type-testing the second argument, which is rather un-Pythonic. BTW, I never knew iter() had a second argument. Having just looked up what it means, I think it was a mistake to put even the existing two functionalities into one] function -- especially given the comment in the docs about it doing "very different things depending on the presence of the second argument"! Greg Ewing, Computer Science Dept, +--------------------------------------+ University of Canterbury, | A citizen of NewZealandCorp, a | Christchurch, New Zealand | wholly-owned subsidiary of USA Inc. | greg@cosc.canterbury.ac.nz +--------------------------------------+
participants (3)
-
Bob Ippolito
-
Greg Ewing
-
Miki Tebeka