Python Forum

Full Version: best pythonic one-time loop
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
i use a one-time loop to surround code where i need a quick way out of a section of code. that way a break is somewhat like a goto end_of_block. the way i have been making a one-time loop is like for x in 'x': or other similar code. is this suitable or is there a better more pythonic way?
well, IMHO any code that requires goto is not pythonic and needs refactoring
the other way to do it is a function. the return is the get out of here operation. loops already have break as a way to get out. but a if block does not. this is just making it possible to do break. this is not a goto.

python cannot do complex conditionals on one line (few languages can and the code ends up so ugly a multi-line way is preferred, especially if there are loops in the complex conditional).