Re: reseting an iterator

2009-05-22 Thread J. Clifford Dyer
On Fri, 2009-05-22 at 10:54 -0700, Jan wrote: > This produces an error because by definition of for-loops > it is executed the same way as: > > temp_iterator = iter(y) # temp_iterator is y > while True: > try: > print(next(temp_iterator)) # temp_iterator does not support > __next__() >

Re: reseting an iterator

2009-05-22 Thread J. Cliff Dyer
On Fri, 2009-05-22 at 10:54 -0700, Jan wrote: > On May 22, 9:46 am, "J. Cliff Dyer" wrote: > > > You don't need a reset method. There is no hard and fast rule that > > __iter__ must return the object itself. It just needs to return an > > iterator. > > I disagree. > If ITRATOR is a true ite

Re: reseting an iterator

2009-05-22 Thread Jan
On May 22, 9:46 am, "J. Cliff Dyer" wrote: > You don't need a reset method.  There is no hard and fast rule that > __iter__ must return the object itself.  It just needs to return an > iterator.   I disagree. If ITRATOR is a true iterator, ITRATOR.__iter__() must return ITERATOR. If ITERABLE is

Re: reseting an iterator

2009-05-22 Thread J. Cliff Dyer
On Wed, 2009-05-20 at 11:35 -0700, Jan wrote: > Wouldn't it be easy for Python to implement generating functions so > that the iterators they return are equipped with a __reset__() method? > > Here is the context of this question. > > Python documentation defines a "iterator" as an object ITERAT

Re: reseting an iterator

2009-05-21 Thread Terry Reedy
norseman wrote: Terry Reedy wrote: I will clarify by starting over with current definitions. Ob is an iterator iff next(ob) either returns an object or raises StopIteration and continues to raise StopIteration on subsequent calls. Ob is an iterable iff iter(ob) raturns an iterator. It is in

Re: reseting an iterator

2009-05-21 Thread norseman
Terry Reedy wrote: I will clarify by starting over with current definitions. Ob is an iterator iff next(ob) either returns an object or raises StopIteration and continues to raise StopIteration on subsequent calls. Ob is an iterable iff iter(ob) raturns an iterator. It is intentional that th

Re: reseting an iterator

2009-05-21 Thread Terry Reedy
I will clarify by starting over with current definitions. Ob is an iterator iff next(ob) either returns an object or raises StopIteration and continues to raise StopIteration on subsequent calls. Ob is an iterable iff iter(ob) raturns an iterator. It is intentional that the protocol definitio

Re: reseting an iterator

2009-05-21 Thread norseman
Terry Reedy wrote: Jan wrote: Wouldn't it be easy for Python to implement generating functions so that the iterators they return are equipped with a __reset__() method? No. Such a method would have to poke around in the internals of the __next__ function in implementation specific ways. The

Re: reseting an iterator

2009-05-21 Thread Duncan Booth
Steven D'Aprano wrote: > On Wed, 20 May 2009 11:35:47 -0700, Jan wrote: > >> Wouldn't it be easy for Python to implement generating functions so that >> the iterators they return are equipped with a __reset__() method? > > No. > > def gen(): > for name in os.listdir('.'): > yield o

Re: reseting an iterator

2009-05-20 Thread Steven D'Aprano
On Wed, 20 May 2009 11:35:47 -0700, Jan wrote: > Wouldn't it be easy for Python to implement generating functions so that > the iterators they return are equipped with a __reset__() method? No. def gen(): for name in os.listdir('.'): yield open(name).read() os.remove(name)

Re: reseting an iterator

2009-05-20 Thread Terry Reedy
Jan wrote: Wouldn't it be easy for Python to implement generating functions so that the iterators they return are equipped with a __reset__() method? No. Such a method would have to poke around in the internals of the __next__ function in implementation specific ways. The values used to ini

Re: reseting an iterator

2009-05-20 Thread Alan G Isaac
Jan wrote: Wouldn't it be easy for Python to implement generating functions so that the iterators they return are equipped with a __reset__() method? Use ``send``: http://docs.python.org/whatsnew/2.5.html#pep-342-new-generator-features Remember, there may be no underlying sequence object for a

Re: reseting an iterator

2009-05-20 Thread Jan
On May 20, 2:48 pm, Jan wrote: Iterators can also be produced by iter(ITERABLE) which could mnufacture them with a __reset__. Jan -- http://mail.python.org/mailman/listinfo/python-list

Re: reseting an iterator

2009-05-20 Thread Jan
On May 20, 2:35 pm, Jan wrote: OOPS, I have pressed some keys and the message went out before It was finished. Here is the last fragment: So, one can define iterators by defining a class whose objects have methods __iter__ and __next__ -- with this approach it is easy to add some __reset__ metho

reseting an iterator

2009-05-20 Thread Jan
Wouldn't it be easy for Python to implement generating functions so that the iterators they return are equipped with a __reset__() method? Here is the context of this question. Python documentation defines a "iterator" as an object ITERATOR having methods __next__() and __iter__() such that the