Pythonic do-while loop

Although python does not explicitly allow do-while loops, there are at least 3 reasonable ways to implement them:

1)

while True: #loop body if not expr(): break 

2)

 x = True while x: #loop body x = expr() 

3)

 def f(): #loop body f() while expr(): f() 

Not to mention the other methods mentioned here (for example, coroutines, try-except clauses, iterators, etc.) which, I believe, are not pythons in most conditions. I even see some answers, arguing that do-while loops are not pythonic, but I don’t know a common alternative.

What is the most pythonic method? They all have their own strangeness: 1) begins with an infinite loop, 2) first creates an opaque variable, and 3) defines a new function. Does anyone have a better method?

+4
source share

Source: https://habr.com/ru/post/1445829/


All Articles