Re: StopIteration in the if clause of a generator expression

2005-04-05 Thread Raymond Hettinger
> > [Steven Bethard] > > > So do I read this right in preferring > > > [ for in ] > > > over > > > list( for in ) [Raymond Hettinger] > > Yes! [Simon Brunning] > Why? (Serious question. I'm sure that you have a good reason - I just > can't figure out what it is.) > > The generator exp

Re: StopIteration in the if clause of a generator expression

2005-04-05 Thread Duncan Booth
Simon Brunning wrote: > On Apr 5, 2005 2:04 AM, Raymond Hettinger <[EMAIL PROTECTED]> wrote: >> [Steven Bethard] >> > So do I read this right in preferring >> > [ for in ] >> > over >> > list( for in ) >> >> Yes! > > Why? (Serious question. I'm sure that you have a good reason - I ju

Re: StopIteration in the if clause of a generator expression

2005-04-05 Thread Simon Brunning
On Apr 5, 2005 2:04 AM, Raymond Hettinger <[EMAIL PROTECTED]> wrote: > [Steven Bethard] > > So do I read this right in preferring > > [ for in ] > > over > > list( for in ) > > Yes! Why? (Serious question. I'm sure that you have a good reason - I just can't figure out what it is.) Th

Re: StopIteration in the if clause of a generator expression

2005-04-04 Thread Raymond Hettinger
> > Use a genexp when you need a generator > > and use a listcomp when you need a list. [Steven Bethard] > So do I read this right in preferring > [ for in ] > over > list( for in ) Yes! Raymond -- http://mail.python.org/mailman/listinfo/python-list

Re: StopIteration in the if clause of a generator expression

2005-04-04 Thread Steven Bethard
Raymond Hettinger wrote: [Steven Bethard] and I often find myself alternating between the two when I can't decide which one seems more Pythonic. Both are pythonic. Use a genexp when you need a generator and use a listcomp when you need a list. So do I read this right in preferring [ for in ] o

Re: StopIteration in the if clause of a generator expression

2005-04-04 Thread Raymond Hettinger
[Steven Bethard] > and I often find myself alternating > between the two when I can't decide which one seems more Pythonic. Both are pythonic. Use a genexp when you need a generator and use a listcomp when you need a list. Raymond Hettinger -- http://mail.python.org/mailman/listinfo/python-l

Re: StopIteration in the if clause of a generator expression

2005-04-03 Thread [EMAIL PROTECTED]
This is all just making everything far too complicated. What you really want to do is quite simple: import itertools def condition(x): return x < 5 list(itertools.takewhile(condition, (i for i in range(10 The 'Stop Iteration In Generator Expression' problem was solved in the language that Li

Re: StopIteration in the if clause of a generator expression

2005-04-03 Thread Steven Bethard
Raymond Hettinger wrote: [Peter Otten] Do you see any chance that list comprehensions will be redefined as an alternative spelling for list()? Not likely. It is possible that the latter spelling would make it possible for Py3.0. eliminate list comps entirely. However, they are very popular and p

Re: StopIteration in the if clause of a generator expression

2005-04-03 Thread Raymond Hettinger
[Peter Otten] > Do you see any chance that list comprehensions will be redefined as an > alternative spelling for list()? Not likely. It is possible that the latter spelling would make it possible for Py3.0. eliminate list comps entirely. However, they are very popular and practical, so my bet i

Re: StopIteration in the if clause of a generator expression

2005-04-03 Thread Peter Otten
jfj wrote: >> To make it a bit clearer, a StopIteration raised in a generator >> expression silently terminates that generator: > > *any* exception raised from a generator, terminates the generator Yeah, but StopIteration is the only expected exception and therefore the only one that client code

Re: StopIteration in the if clause of a generator expression

2005-04-03 Thread Peter Otten
Raymond Hettinger wrote: (quoting Bengt) >> I assumed that all standard sequence consumers (including list, of >> course) would intercept the StopIteration of a sequence given them in the >> form of a generator expression, so your lyst example would have an >> analogue for other sequence consumer

Re: StopIteration in the if clause of a generator expression

2005-04-01 Thread Raymond Hettinger
> I assumed that all standard sequence consumers (including list, of course) would intercept > the StopIteration of a sequence given them in the form of a generator expression, so your > lyst example would have an analogue for other sequence consumers as well, right? > I.e., there's not a hidden li

Re: StopIteration in the if clause of a generator expression

2005-04-01 Thread Bengt Richter
On Fri, 01 Apr 2005 16:34:32 GMT, "Raymond Hettinger" <[EMAIL PROTECTED]> wrote: >[Peter Otten] >> a StopIteration raised in a generator expression >> silently terminates that generator: >> >> >>> def stop(): raise StopIteration >> ... >> >>> list(i for i in range(10) if i < 5 or stop()) >> [0, 1,

Re: StopIteration in the if clause of a generator expression

2005-04-01 Thread Raymond Hettinger
[Peter Otten] > a StopIteration raised in a generator expression > silently terminates that generator: > > >>> def stop(): raise StopIteration > ... > >>> list(i for i in range(10) if i < 5 or stop()) > [0, 1, 2, 3, 4] > > In a list comprehension, on the other hand, it is propagated: > > >>> [i for

Re: StopIteration in the if clause of a generator expression

2005-04-01 Thread Carl Banks
Peter Otten wrote: > To confuse a newbies and old hands alike, Bengt Richter wrote: > > > Need something more straightforward, e.g., a wrapped one-liner: > > > > >>> def guess(n=3): print ("You're right!", 'No more tries for > > >>> you!!!')[n-1 in > > ...(x for x in xrange(n) for t in [raw_

Re: StopIteration in the if clause of a generator expression

2005-04-01 Thread jfj
Peter Otten wrote: To confuse a newbies and old hands alike, Bengt Richter wrote: got me for one:) To make it a bit clearer, a StopIteration raised in a generator expression silently terminates that generator: *any* exception raised from a generator, terminates the generator jfj -- http://mail.pyt

StopIteration in the if clause of a generator expression

2005-04-01 Thread Peter Otten
To confuse a newbies and old hands alike, Bengt Richter wrote: > Need something more straightforward, e.g., a wrapped one-liner: > > >>> def guess(n=3): print ("You're right!", 'No more tries for > >>> you!!!')[n-1 in > ...(x for x in xrange(n) for t in [raw_input('Guess my name: > ')=='B