Is there an expression for an infinite generator?

Is there a direct generator expression that can produce infinite elements?

This is a purely theoretical question. No need for a β€œpractical” answer here :)




For example, it is easy to do the final generation:

my_gen = (0 for i in xrange(42)) 

However, to make it infinite, I need to "pollute" my namespace with a fake function:

 def _my_gen(): while True: yield 0 my_gen = _my_gen() 

The execution of actions in a separate file and import -ing are not taken into account later.




I also know that itertools.repeat does just that. I am curious if there is a one line solution without this.

+75
python iterator generator infinite-loop
Apr 20 2018-11-21T00:
source share
8 answers
 for x in iter(int, 1): pass 
  • Two arguments iter = argument with null argument, called + sender
  • int() always returns 0

Therefore, iter(int, 1) is an infinite iterator. Obviously, there are a huge number of variations on this topic (especially after adding lambda to the mix). One option for a specific note is iter(f, object()) , because the newly created object is used, since the value of the sentinel element almost guarantees an infinite iterator, regardless of the called, used as the first argument.

+83
Apr 21 2018-11-11T00:
source share

itertools contains three infinite generators:

I do not know others in the standard library.




Since you asked for a single liner:

 __import__("itertools").count() 
+116
Apr 20 2018-11-21T00:
source share

you can iterate over a called call that returns a constant that is always different from iter () sentinel

 g1=iter(lambda:0,1) 
+9
Apr 21 '11 at 5:20
source share

Your OS may provide something that can be used as an infinite generator. For example, on linux

 for i in (0 for x in open('/dev/urandom')): print i 

obviously this is not as effective as

 for i in __import__('itertools').repeat(0) print i 
+6
Apr 21 '11 at 12:00 a.m.
source share

No, that does not use an internal infinite iterator defined as a class / function / generator (non-expression, function with yield ). A generator expression is always extracted from anoter iterable and does nothing but filter and display its elements. You cannot move from finite elements to infinite using map and filter , you need a while (or for , which does not end, which is exactly what we cannot use only for and finite iterators).

General: PEP 3142 looks similar in appearance, but upon closer inspection it seems that this still requires a for clause (therefore no (0 while True) for you), i.e. provides only a shortcut to itertools.takewhile .

+4
Apr 20 2018-11-21T00:
source share

Perhaps you could use decorators like this, for example:

 def generator(first): def wrap(func): def seq(): x = first while True: yield x x = func(x) return seq return wrap 

Usage (1):

 @generator(0) def blah(x): return x + 1 for i in blah(): print i 

Usage (2)

 for i in generator(0)(lambda x: x + 1)(): print i 

I think it could be improved to get rid of those ugly ones () . However, this depends on the complexity of the sequence you want to create. Generally speaking, if your sequence can be expressed using functions, then all the complexity and syntactic sugar of generators can be hidden inside a decorator or a function like a decorator.

+2
Apr 20 '11 at 10:22
source share

Pretty ugly and crazy (very funny, however), but you can create your own iterator from an expression using some tricks (without having to "pollute" your namespace as needed):

 { print("Hello world") for _ in (lambda o: setattr(o, '__iter__', lambda x:x) or setattr(o, '__next__', lambda x:True) or o) (type("EvilIterator", (object,), {}))() } 
+1
Nov 17 '17 at 20:35
source share
 def iter_cycle(iterator): saved = [] for iterable in iterator: yield iterable saved.append(iterable) while saved: for element in saved: yield element a = iter_cycle(iter([1,2,3])) 

next (a) β†’ 1 next (a) β†’ 2 next (a) β†’ 3

next (a) β†’ 1

(as mentioned, thanks to python documentation https://docs.python.org/2/library/itertools.html#itertools.cycle )

-one
Aug 11 '16 at 11:36
source share



All Articles