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 8616441
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T05:37:14+00:00 2026-06-12T05:37:14+00:00

What is the design pattern behind python like interactive shell . I want to

  • 0

What is the design pattern behind python like interactive shell. I want to do this for my server but I am ending up with lot of if - then- else pattern.

For example, when I start python interpreter I get something like this

Python 2.6.7 (r267:88850, Feb  2 2012, 23:50:20)
[GCC 4.5.3] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> help

After help the prompt changes to help

Welcome to Python 2.6!  This is the online help utility.

If this is your first time using Python, you should definitely check out
the tutorial on the Internet at http://docs.python.org/tutorial/.

Enter the name of any module, keyword, or topic to get help on writing
Python programs and using Python modules.  To quit this help utility and
return to the interpreter, just type "quit".

To get a list of available modules, keywords, or topics, type "modules",
"keywords", or "topics".  Each module also comes with a one-line summary
of what it does; to list the modules whose summaries contain a given word
such as "spam", type "modules spam".

help>

I think this is some king of read-eval loop design.

  • 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-06-12T05:37:15+00:00Added an answer on June 12, 2026 at 5:37 am

    for a REPL, you need a context (an object which stores the REPL’s state), a command parser (which parses input and produces an AST), and a way to map commands to actions (actions are generally just functions that modifies the context and/or produces side effects).

    A simple REPL can be implemented like the following, where context is implemented using a simple dictionary, AST is just the inputted commands split on whitespaces, and a dictionary is used to map commands to actions:

    context = {}
    commands = {}
    
    def register(func):
        """ convenience function to put `func` into commands map """
        # in C++, you cannot introspect the function's name so you would
        # need to map the function name to function pointers manually
        commands[func.__name__] = func
    def parse(s):
        """ given a command string `s` produce an AST """
        # the simplest parser is just splitting the input string,
        # but you can also produce use a more complicated grammer
        # to produce a more complicated syntax tree
        return s.split()
    def do(cmd, commands, context):
        """ evaluate the AST, producing an output and/or side effect """
        # here, we simply use the first item in the list to choose which function to call
        # in more complicated ASTs, the type of the root node can be used to pick actions
        return commands[cmd[0]](context, cmd)
    
    @register
    def assign(ctx, args):
        ctx[args[1]] = args[2]
        return '%s = %s' % (args[1], args[2])
    @register
    def printvar(ctx, args):
        print ctx[args[1]]
        return None
    @register
    def defun(ctx, args):
        body = ' '.join(args[2:])
        ctx[args[1]] = compile(body, '', 'exec')
        return 'def %s(): %s' % (args[1], body)
    @register
    def call(ctx, args):
        exec ctx[args[1]] in ctx
        return None
    # more commands here
    
    context['PS1'] = "> "
    while True:
        # READ
        inp = raw_input(context["PS1"])
    
        # EVAL
        cmd = parse(inp)
        out = do(cmd, commands, context)
    
        # PRINT
        if out is not None: print out
    
        # LOOP
    

    A sample session:

    > assign d hello
    d = hello
    > printvar d
    hello
    > assign PS1 $
    PS1 = $
    $defun fun print d + 'world'
    def fun(): print d + 'world'
    $call fun
    helloworld
    

    with a little bit more trickery, you could even merge the context and commands dictionary together, allowing the shell’s set of commands to be modified in the shell’s language.

    The name of this design pattern, if it has a name, is Read-Eval-Print Loop design pattern; so yeah, your question sorta answers itself.

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

Sidebar

Related Questions

I understand the theory behind the factory design pattern now, but can't seem to
Like any design pattern the Specification Pattern is a great concept but susceptible to
I'm trying to figure out the name behind this design pattern. Basically, you have
Data Access Objects (DAOs) are a common design pattern, and recommended by Sun. But
I want to implement singleten design pattern in iphone code I have one array.
What design pattern might apply to logging? What is normally used in this type
The design pattern the first letter in this acronym stands for is the Single
I'm looking for a design\pattern to my problem. I want to declare a singleton
This questions is not really about code but application design. I have an app
I was looking at design pattern, but got confused between Factory and Abstract Factory

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.