Trouble understand 'While True'

i was looking throught the PySimpleGUI docs and i saw something i have seen a few times bfore in other Python code.

The ‘while true’. This makes sense to me in most contexts where there is a pre-defined ‘break’. But this is what was defined in the docs

while True:                             # The Event Loop
    event, values = window.read() 
    print(event, values)       
    if event == sg.WIN_CLOSED or event == 'Exit':
        break

to me, this means that the event loop will run until the program is closed. In javascript, something like this would freeze my computer (i think?, im just a beginner). Wondering if the python event loop uses less resources or something that makes it easier to run indefinitely.

or i may just be misunderstanding it completely

Generally, I consider while True to be an anti-pattern. You will see it sometimes in special cases though

I think of this as a way to implement an “until” type-loop. For example, you might have a function where you want to divide the input by 2 until it is less than 10. Here you could write

while True:
     input=input/2
     if input < 10:
        break 

So, you can create loop that will run until some conditions are satisfied. I am fairly new to python, so I’m not sure if this is the “correct” way to think about it, but it’s what makes sense to me.

I think in this case it is better like this

while input >= 10:
    input /= 2
1 Like

Based on the short snippet, I’m not sure what other code may be running, but if event == sg.WIN_CLOSED or event == exit may not mean the whole program is closing; it may mean that something like one window in the program is closing. So the while loop may run while a particular window is open and exit once the window is close. But since the program may have many windows, exiting that window does not necessarily mean the program is closed.

One could say everything in programming is a special case. Anti-pattern is a bit far IMO.

One could say all sorts of things. There still exist general purpose rules and best practices.

while true is still, in general, an antipattern. Explicitly infinite loops should be avoided unless you really have a fantastic reason why you must use an infinite loop. Those reasons exist, but having a good reason to use while true is not that common.

1 Like

The break statement is the last line in the code in the example. The conditions for exiting are defined, in this example, in that if statement. It says sg.WIN_CLOSED as one “event”, which sounds like “Window was closed”. The other item in the if statement is an event of “Exit”. I think this example code fragment had a button with text/key of “Exit” . It means if the button with “Exit” on it is clicked.

while True isn’t a rare or unusual construct to find in Python. It’s quite common. GitHub reports 6.9 million instances.

Lots of people love to write infinite loops and hope their break logic will save them. It’s still not really a best practice and it’s easy to accidentally make a while true into a true infinite loop. You do you. I just will continue to avoid that particular antipattern.

How would you write the loop in the example?

event, values = window.read() 
while  event != sg.WIN_CLOSED:                             # The Event Loop
    # DO STUFF
    print(event, values)
    event, values = window.read()

Explicit exist condition that is clear and harder to break.

Or

def event_run():
    event, values = window.read() 
    if event == sg.WIN_CLOSED:  return false
    # DO STUFF
    print(event, values)
    return true

repeat_loop = True
while repeat_loop: # Event Loop
    repeat_loop = event_run()

Same thing but perhaps better annotated.

I see… basically… a “while not done”. Does this not suffer the same “forgetful programmer” problem of the programmer forgetting to set done = True in the loop, resulting in the feared infinite loop?

done = False
while not done:                             # The Event Loop
    event, values = window.read() 
    print(event, values)       
    if event == sg.WIN_CLOSED or event == 'Exit':
        done = True

You can still make mistakes, sure, but you are aren’t explicitly say ‘run the loop forever’.

Your ‘while not done’ version is still hiding the exit condition and using the same ‘I hope I break right’ logic. The ‘while the exit condition is false’ clearly highlights what will cause the loop to exit.

Errors will always exist. I prefer to move away from errors while also moving towards clarity.

You clearly prefer ‘while true’. Like I said, if you like it, do it. I don’t and won’t recommend the pattern to others.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.