liveBook · Manning

Application loop

As explained in Practices of the Python Pro

An application loop is a control-flow pattern that repeatedly presents an application's interface and processes user actions until termination.

An application loop repeatedly shows the available actions, accepts a user choice, runs that action, and waits before returning to the menu. It keeps an interactive program usable for several actions while leaving the menu’s exit option in control. section 6.3.3

A small application loop

def loop(): print_options(options) chosen_option = get_option_choice(options) chosen_option.choose() input('Press ENTER to return to menu') while True: loop() section 6.3.3

A practical loop coordinates these steps:

  • Present menu options connected to commands. section 6.3.3
  • Validate the user’s choice and select the matching option. section 6.3.3
  • Clear the screen, run the chosen action, and pause before the next menu. section 6.3.3
def loop():                                           #1
    # All the steps for showing/selecting options
    ...
    _ = input('Press ENTER to return to menu')        #2

if __name__ == '__main__':
    commands.CreateBookmarksTableCommand().execute()

    while True:                                       #3
        loop()

Shows the loop method, the pause returning users to the menu, and the outer repetition. section 6.3.3

How Practices of the Python Pro covers it

How deep it goes
Explains the loop’s placement and interaction steps with pseudocode, while deferring detailed command implementations.
Examples
Uses a bookmark application with add, listing, deletion, and quit menu options.
What you can do after
Build it from scratch by separating a loop method from initialization and repeating it around the interactive application.
How it connects
Connects the application loop to the presentation layer, menus, input validation, and command objects.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Practices of the Python Pro. The section links point to the parts of the book it draws on.