Re: Generator question

2019-03-14 Thread Pierre Reinbold
Wow, thank you Ian for this very detailed answer, and thank you for taking the time for that! Much appreciated! If I get this right, I have to somehow fix the value of a_list during the loop, like when you use the classic default value argument trick with lambdas (damn, I should have thought of th

Re: Generator question

2019-03-14 Thread Pierre Reinbold
Le 14/03/19 à 10:45, Peter Otten a écrit : > Pierre Reinbold wrote: > >> Wow, thank you Ian for this very detailed answer, and thank you for taking >> the time for that! Much appreciated! >> >> If I get this right, I have to somehow fix the value of a_list during the >> loop, like when you use the

Re: Generator question

2019-03-14 Thread Peter Otten
Pierre Reinbold wrote: > Wow, thank you Ian for this very detailed answer, and thank you for taking > the time for that! Much appreciated! > > If I get this right, I have to somehow fix the value of a_list during the > loop, like when you use the classic default value argument trick with > lambda

Re: Generator question

2019-03-14 Thread Pierre Reinbold
Wow, thank you Ian for this very detailed answer, and thank you for taking the time for that! Much appreciated! If I get this right, I have to somehow fix the value of a_list during the loop, like when you use the classic default value argument trick with lambdas (damn, I should have thought of th

Re: Generator question

2019-03-13 Thread Ian Kelly
You're basically running into this: https://docs.python.org/3/faq/programming.html#why-do-lambdas-defined-in-a-loop-with-different-values-all-return-the-same-result To see why, let's try disassembling your function. I'm using Python 3.5 here, but it shouldn't make much of a difference. py> import

Re: Generator Question

2011-12-24 Thread GZ
I see. Thanks for the clarification. On Dec 22, 12:35 am, Steven D'Aprano wrote: > On Wed, 21 Dec 2011 21:45:13 -0800, GZ wrote: > > Now the question here is this: > > > def h(): > >     if condition=true: > >        #I would like to return an itereator with zero length > >     else: > >        f

Re: Generator Question

2011-12-21 Thread Steven D'Aprano
On Wed, 21 Dec 2011 21:45:13 -0800, GZ wrote: > Now the question here is this: > > def h(): > if condition=true: >#I would like to return an itereator with zero length > else: >for ...: yield x > > In other words, when certain condition is met, I want to yield nothing. >

Re: Generator Question

2011-12-21 Thread Chris Angelico
On Thu, Dec 22, 2011 at 4:45 PM, GZ wrote: > def h(): >    if condition=true: >       #I would like to return an itereator with zero length >    else: >       for ...: yield x Easy. Just 'return' in the conditional. >>> def h(): if condition: return for i in range

Re: Generator Question

2011-12-21 Thread Steven D'Aprano
On Wed, 21 Dec 2011 21:45:13 -0800, GZ wrote: > Hi, > > I am wondering what would be the best way to return an iterator that has > zero items. return iter([]) > I just noticed the following two are different: > > def f(): >pass That creates a function that does nothing, and then returns

Re: Generator question

2010-12-23 Thread Nobody
On Wed, 22 Dec 2010 15:49:31 -0800, Dan Stromberg wrote: > def generator(): > i = 0 > while True: > yield i > i += 1 Shorter version: from itertools import count as generator -- http://mail.python.org/mailman/listinfo/python-list

Re: Generator question

2010-12-22 Thread Victor Eijkhout
Dan Stromberg wrote: > You likely want a class variable: Sounds like an elegant solution. Thanks! Victor. -- Victor Eijkhout -- eijkhout at tacc utexas edu -- http://mail.python.org/mailman/listinfo/python-list

Re: Generator question

2010-12-22 Thread Dan Stromberg
On Wed, Dec 22, 2010 at 3:15 PM, Victor Eijkhout wrote: > So I have a generator, either as a free function or in a class and I > want to generate objects that are initialized from the generated things. > > def generator(): >        for whatever: >                yield something > class Object(): >

Re: Generator question

2010-12-22 Thread Emile van Sebille
On 12/22/2010 3:15 PM Victor Eijkhout said... So I have a generator, either as a free function or in a class and I want to generate objects that are initialized from the generated things. def generator(): for whatever: yield something class Object(): def __init

Re: Generator question

2006-11-27 Thread Peter Otten
Mathias Panzenboeck wrote: > Robert Kern wrote: >> Timothy Wu wrote: >>> Hi, >>> >>> Using generator recursively is not doing what I expect: >>> >>> def test_gen(x): >>> yield x >>> x = x - 1 >>> if x != 0: >>> test_gen(x) >> >> The only thing that the last line does is *creat

Re: Generator question

2006-11-26 Thread Mathias Panzenboeck
Robert Kern wrote: > Timothy Wu wrote: >> Hi, >> >> Using generator recursively is not doing what I expect: >> >> def test_gen(x): >> yield x >> x = x - 1 >> if x != 0: >> test_gen(x) > > The only thing that the last line does is *create* a new generator object. You > need to a

Re: Generator question

2006-11-26 Thread Timothy Wu
On 11/26/06, Robert Kern <[EMAIL PROTECTED]> wrote: The only thing that the last line does is *create* a new generator object. You need to actually iterate over it and yield its values. E.g. In [2]: def test_gen(x): ...: yield x ...: x -= 1 ...: if x != 0: ...:

Re: Generator question

2006-11-26 Thread Robert Kern
Timothy Wu wrote: > Hi, > > Using generator recursively is not doing what I expect: > > def test_gen(x): > yield x > x = x - 1 > if x != 0: > test_gen(x) The only thing that the last line does is *create* a new generator object. You need to actually iterate over it and yield