title: Pythonic do-while loop
body: <p>While python doesn't explicitly allow do-while loops, there are at least 3 reasonable ways to implement them:</p>
  
  <p>1) </p>
  
  <pre><code>while True:
      #loop body
      if not expr():
          break
  </code></pre>
  
  <p>2)</p>
  
  <pre><code>x = True
  while x:
      #loop body
      x = expr()
  </code></pre>
  
  <p>3)</p>
  
  <pre><code>def f():
      #loop body
  
  f()
  while expr():
      f()
  </code></pre>
  
  <p>Not to mention other methods mentioned <a href="https://stackoverflow.com/questions/743164/do-while-loop-in-python">here</a> (e.g. coroutines, try-except clauses, iterators, etc), that I assume are non-pythonic under most conditions. I even see some answers arguing that do-while loops are non-pythonic, but I don't know a generic alternative.</p>
  
  <p>Which method is most pythonic? They all have their oddity: 1) begins with an infinite loop, 2) creates a opaque variable at first, and 3) defines a new function. Does anyone have a better method?</p>