Re: list() strange behaviour
On 1/23/2021 2:54 AM, Unknown wrote: Le 20/12/2020 à 21:00, danilob a écrit : b = ((x[0] for x in a)) There is a useless pair of parenthesis b = (x[0] for x in a) b is a GENERATOR expression first list(b) calls next method on b repetedly until b is empty. So it provides the "content" of b second list(b) provides nothing since b is empty (there is no reset on generators) There is for generator *functions*, which some people also call generators. For generator expressions, the interpreter makes an anonymous generator function, calls it, keeping the returned generator. It deletes the function and 'returns' the generator as the value of the function. If one want to iterator twice, write a proper named generator function with a docstring. In this case def gf(a): "Return a generator that yields the first item of each sequence in a." for x in a: yield x[0] -- Terry Jan Reedy -- https://mail.python.org/mailman/listinfo/python-list
RE: list() strange behaviour
I am wondering how hard it would be to let some generators be resettable? I mean if you have a generator with initial conditions that change as it progresses, could it cache away those initial conditions and upon some signal, simply reset them? Objects of many kinds can be set up with say a reinit() method. I am not saying Python needs such a language change for generators as in many programs you could just recreate a new instance of a generator. But there may be places where by the time the generator is used, the original is not known. Or, there are places where you want to lengthen something to match another by repeatedly copying the same sequence as many times as needed. If a generator finishes, you want it to restart with the same sequence until you stop asking. This is just a thought, not a request for such a feature. -Original Message- From: Python-list On Behalf Of ast Sent: Saturday, January 23, 2021 2:54 AM To: python-list@python.org Subject: Re: list() strange behaviour Le 20/12/2020 à 21:00, danilob a écrit : > > > b = ((x[0] for x in a)) > There is a useless pair of parenthesis b = (x[0] for x in a) b is a GENERATOR expression first list(b) calls next method on b repetedly until b is empty. So it provides the "content" of b second list(b) provides nothing since b is empty (there is no reset on generators) -- https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list
Re: list() strange behaviour
On 23Jan2021 12:20, Avi Gross wrote: >I am wondering how hard it would be to let some generators be resettable? > >I mean if you have a generator with initial conditions that change as it >progresses, could it cache away those initial conditions and upon some >signal, simply reset them? Objects of many kinds can be set up with say a >reinit() method. On reflection I'd do this with a class: class G: def __init__(self, length): self.length = length self.pos = 0 def __next__(self): pos = self.pos if pos >= length: raise StopIteration() self.pos += 1 return pos def __iter__(self): return self then: g = G(9) for x in g: print(x) if x == 5: g.pos = 7 You could either fiddle the iterable class instance attributes directly as above or provide a reset method of some kind. If you don't need to fiddle/reset you can just write: for x in G(9): The two step above is so we have "g" to hand to do the fiddling. Cheers, Cameron Simpson -- https://mail.python.org/mailman/listinfo/python-list
Re: list() strange behaviour
On Sun, Jan 24, 2021 at 8:49 AM Cameron Simpson wrote: > > On 23Jan2021 12:20, Avi Gross wrote: > >I am wondering how hard it would be to let some generators be resettable? > > > >I mean if you have a generator with initial conditions that change as it > >progresses, could it cache away those initial conditions and upon some > >signal, simply reset them? Objects of many kinds can be set up with say a > >reinit() method. > > On reflection I'd do this with a class: > > class G: > def __init__(self, length): > self.length = length > self.pos = 0 > > def __next__(self): > pos = self.pos > if pos >= length: > raise StopIteration() > self.pos += 1 > return pos > > def __iter__(self): > return self > Yep, or more conveniently: class G: def __init__(self, length): self.length = length self.pos = 0 def __iter__(self): while self.pos < self.length: yield self.pos self.pos += 1 The only significant part is the fact that your generator function has *external* state, which is what allows you to manipulate it. Generator functions are still way cleaner for most purposes than handwritten iterator classes. ChrisA -- https://mail.python.org/mailman/listinfo/python-list
Re: list() strange behaviour
> On 23 Jan 2021, at 17:23, Avi Gross via Python-list > wrote: > > I am wondering how hard it would be to let some generators be resettable? That is generally thought to be a bad thing in OOD. Classes that reset are usually a code smell and often a source of bugs. Why not just assign a new generate into the variable that is used to access the generator. The avoids the need for the complications of reset logic. Barry > > I mean if you have a generator with initial conditions that change as it > progresses, could it cache away those initial conditions and upon some > signal, simply reset them? Objects of many kinds can be set up with say a > reinit() method. > > I am not saying Python needs such a language change for generators as in > many programs you could just recreate a new instance of a generator. But > there may be places where by the time the generator is used, the original is > not known. Or, there are places where you want to lengthen something to > match another by repeatedly copying the same sequence as many times as > needed. If a generator finishes, you want it to restart with the same > sequence until you stop asking. > > This is just a thought, not a request for such a feature. > > -Original Message- > From: Python-list On > Behalf Of ast > Sent: Saturday, January 23, 2021 2:54 AM > To: python-list@python.org > Subject: Re: list() strange behaviour > >> Le 20/12/2020 à 21:00, danilob a écrit : >> b = ((x[0] for x in a)) > > There is a useless pair of parenthesis > > b = (x[0] for x in a) > > b is a GENERATOR expression > > first list(b) calls next method on b repetedly until b is empty. > So it provides the "content" of b > > second list(b) provides nothing since b is empty (there is no reset on > generators) > > > > > -- > https://mail.python.org/mailman/listinfo/python-list > > -- > https://mail.python.org/mailman/listinfo/python-list -- https://mail.python.org/mailman/listinfo/python-list