1-liner to iterate over infinite sequence of integers?
Peter Hansen
peter at engcorp.com
Thu Oct 13 21:42:09 EDT 2005
Erik Max Francis wrote:
> The negative integers are being descriminated against! I demand justice!
>
> def integers():
> yield 0
> x = 1
> while True:
> yield x
> yield -x
> x += 1
>
> ... which is also not a bad demonstration of how the integers are
> countably infinite.
For some reason I was unable to avoid seeing that one can save a yield
there (not that they're a scarce resource, mind you):
def integers():
x = 0
while True:
yield -x
x += 1
yield x
I know that shouldn't win any readability awards...
-Peter
More information about the Python-list
mailing list