"...">

Correct way to create a shell in python environment?

I am trying to create a shell such as an environment where the user is presented with "→>" and can enter any of several predefined commands. However, the only way I can do this is by matching dictionary commands -> code and python "exec".

Is there a better way to do this?

+2


a source to share


2 answers


The standard library module cmd is used for this .



If you end up rolling your own solution, there is no need to include exec. Dictionary-to-code conversion commands must map strings to strings. It can map strings to actual functions. In fact, a class is a mapping of strings to code (method names to define methods).

+6


a source


If this is an interactive Python interpreter that you are making, check out the code .



0


a source







All Articles