Python中可能有无限循环? [英] Infinite for loops possible in Python?

查看:229
本文介绍了Python中可能有无限循环?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在中为循环获取一个无限循环?



我的猜测是, Python中的无限循环。

解决方案

Python中一个无限循环的典型例子是:

 而真:
传递

$ b $

$ b $ p $ def infinity ():
而True:
yield

这可以如下使用:

  for _ in infinity():
pass


Is it possible to get an infinite loop in for loop?

My guess is that there can be an infinite for loop in Python. I'd like to know this for future references.

解决方案

The quintessential example of an infinite loop in Python is:

while True:
    pass

To apply this to a for loop, use a generator (simplest form):

def infinity():
    while True:
        yield

This can be used as follows:

for _ in infinity():
    pass

这篇关于Python中可能有无限循环?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆