Simulating C-style for loops in python

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

    What about:

    date = start
    
    while date < end:
    
        if not another_calendar.is_holiday(date):
            # ... do stuff...
    
        date = calendar.next_quarter_end(date)
    

    But if you use that particular construct often, you're better off defining the generator once and re-using it as you did in your question.

    (The fact is, since they're different languages, you can't possibly have every construct in C map to a more compact construct in Python. It's like claiming to have a compression algorithm that works equally well on all random inputs.)

提交回复
热议问题