Simulating C-style for loops in python

后端 未结 6 1468
孤独总比滥情好
孤独总比滥情好 2020-12-13 05:05

(even the title of this is going to cause flames, I realize)

Python made the deliberate design choice to have the for loop use explicit iterables, with

6条回答
  •  佛祖请我去吃肉
    2020-12-13 05:30

    This is the best I can come up with:

    def cfor(first,test,update):
        while test(first):
            yield first
            first = update(first)
    
    def example(blah):
        print "do some stuff"
        for i in cfor(0,lambda i:i

    I wish python had a syntax for closured expressions.

    Edit: Also, note that you only have to define cfor once (as opposed to your complicated_iterator function).

提交回复
热议问题