开发者

Python generator that returns the same thing forever

开发者 https://www.devze.com 2023-01-08 05:12 出处:网络
I\'m looking for a standard function that does this: def Forever(v): while True: yield v It seems so trivial I can\'t believe there isn\'t a standard version.

I'm looking for a standard function that does this:

def Forever(v):
  while True:
    yield v

It seems so trivial I can't believe there isn't a standard version.

For that matter anyone know of a good link to a list of all the stand开发者_如何学Pythonard generator functions?


itertools.repeat(x[, count]) repeats x a finite number of times if told how many times, otherwise repeats forever.

For a general list of all of the itertools generator functions, see here:

http://docs.python.org/library/itertools.html


Your are looking for itertools.repeat(object[, times]):

Make an iterator that returns object over and over again. Runs indefinitely unless the times argument is specified.

0

精彩评论

暂无评论...
验证码 换一张
取 消