Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6900897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T07:36:18+00:00 2026-05-27T07:36:18+00:00

I’m writing a small program that prints a menu screen with a list of

  • 0

I’m writing a small program that prints a menu screen with a list of options. If you choose one of the options, the screen is cleared and you’re taken to the next menu.

Right now, I’ve made a base Menu class and given it a method draw() which draws the options. I then want to add a another method handle_options() which takes user input and then whisks the user off to the appropriate choice.

At the moment, I am creating a series of subclasses like class MainMenu(Menu) and class OptionsMenu(Menu). When handle_options() is looping, I’ll create an instance of the appropriate Menu class, and draw it, then loop into option choices.

But the thing is, I don’t want to have to define this handle_options() method for each variant of Menu. So I ask:

a) Is there a design pattern or better way of doing this kind of thing I can learn from?

b) Can I use something like getattr(self, "myfunc")() to call a function name I have stored in an array, so I can genericise the handle_options() method and define it in the base Menu class, rather than for each subclass?

tl;dr – What kinds of examples or patterns can be used to build a simple text-based menu system?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T07:36:19+00:00Added an answer on May 27, 2026 at 7:36 am

    An alternative to defining handle_options for every subclass of Menu is to define a dict that maps an option to a function call:

    class Menu(object):
       def __init__(self):
          # Ideally, self._options would be an empty dict for the base class.
          # This is just for the sake of example.
          self._options = {'a': self.optionA,
                           'b': self.optionB}
    
       def handle_options(self, option):
          if option not in self._options:
             print "Invalid option"
             # re-draw
             return
    
          self._options[option]()
    
       def optionA(self):
          print "option A"
    
       def optionB(self):
          print "option B"
    

    Now your subclasses simply have to redefine self._options and provide the appropriate methods associated with each option:

    class SubMenu(Menu):
       def __init__(self):
          Menu.__init__(self)
    
          self._options = {'c': self.optionC,
                           'd': self.optionD}
    
       def optionC(self):
          # ...
    
       def optionD(self):
          # ...
    

    You can also expand on self._options to provide text description, for example, so your draw function can simply iterate through the options printing each one. Take a look at how to use argparse to infer a good design.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I need a function that will clean a strings' special characters. I do NOT
I am writing an app with both english and french support. The app requests
I'm trying to create an if statement in PHP that prevents a single post
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.