Simulating C-style for loops in python

后端 未结 6 1436
孤独总比滥情好
孤独总比滥情好 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:39

    Heh:

    def forre(a,i,c,top,increment,run):
        increment = increment.replace("++","+=1").replace("--","-=1").replace("; ","")
        while i != top:
            try: exec(run)
            except: print "error: "; print run
            try: exec(increment)
            except: print "error: "; print increment
    
    forre("int i=",0,"; i<",6,"; i++", 
        "print i"
        )
    

提交回复
热议问题