Is there an expression for an infinite iterator?
Is there a straight-forward expression that can produce an infinite iterator?
This is a purely theoretical question. No need for a "practical" answer here :)
For example, it is easy to use a generator expression to make a finite iterator:
my_gen = (0 for i in xrange(42))
However, to make an infinite one I need to "pollute" my namespace with a bogus function:
def _my_gen():
while True:
yield 0
my_gen = _my_gen()
Doing things in a separate file and import
-ing later doesn't count.
I also know that itertools.repeat
does exactly this. I'm curious if there is a one-liner solution without that.
itertools
provides three infinite iterators:
-
count(start=0, step=1)
: 0, 1, 2, 3, 4, ... -
cycle(p)
: p[0], p[1], ..., p[-1], p[0], ... -
repeat(x, times=∞)
: x, x, x, x, ...
I don't know of any others in the standard library.
Since you asked for a one-liner:
__import__("itertools").count()
for x in iter(int, 1): pass
- Two-argument
iter
= zero-argument callable + sentinel value -
int()
always returns0
Therefore, iter(int, 1)
is an infinite iterator. There are obviously a huge number of variations on this particular theme (especially once you add lambda
into the mix). One variant of particular note is iter(f, object())
, as using a freshly created object as the sentinel value almost guarantees an infinite iterator regardless of the callable used as the first argument.
you can iterate over a callable returning a constant always different than iter()'s sentinel
g1=iter(lambda:0, 1)
Your OS may provide something that can be used as an infinite generator. Eg on linux
for i in (0 for x in open('/dev/urandom')):
print i
obviously this is not as efficient as
for i in __import__('itertools').repeat(0)
print i