i=2; lst=[i**=2 while i<1000]

Sion Arrowsmith siona at chiark.greenend.org.uk
Tue Dec 6 12:47:37 EST 2005


 <bonono at gmail.com> wrote:
>Bengt Richter wrote:
>>  >>> list(iter(lambda b=[2]:b.append(b[0]**2) or b[0]<1000 and b.pop(0) or None, None))
>>  [2, 4, 16, 256]
>out of curiosity, what stops the iterator ?

<http://docs.python.org/lib/built-in-funcs.html>:

iter(o, sentinel) [ ... ]
The iterator created in this case will call o with no arguments for
each call to its next() method; if the value returned is equal to
sentinel, StopIteration will be raised, otherwise the value will be
returned.

In this case, o is:

lambda b=[2]:b.append(b[0]**2) or b[0]<1000 and b.pop(0) or None

which returns None when b[0]>=1000 (None or (False and _) or None
evaluates to the last None).

-- 
\S -- siona at chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |    -- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump



More information about the Python-list mailing list