emperorspride188
emperorspride188

Reputation: 889

How does Python's interactive mode work?

I want to know how Python interactive mode works. Usually when you run Python script on CPython it will go trough the process of lexical analysis, parsing, gets compiled into .pyc file, and finally the .pyc file is interpreted.

Does this 4-step process happen while using interactive mode also, r is there a more efficient way of implementing?

Upvotes: 3

Views: 2472

Answers (2)

Sanghyun Lee
Sanghyun Lee

Reputation: 23092

From the article Is Python interpreted or compiled? Yes.

Another important Python feature is its interactive prompt. You can type Python statements and have them immediately executed. This interactivity is usually missing in "compiled" languages, but even at the Python interactive prompt, your Python is compiled to bytecode, and then the bytecode is executed. This immediate execution, and Python' lack of an explicit compile step, are why people call the Python executable, "the Python interpreter."

Upvotes: 0

Meghdeep Ray
Meghdeep Ray

Reputation: 5537

Python has two basic modes: normal and interactive. The normal mode is the mode where the scripted and finished .py files are run in the Python interpreter. Interactive mode is a command line shell which gives immediate feedback for each statement, while running previously fed statements in active memory. As new lines are fed into the interpreter, the fed program is evaluated both in part and in whole.
The same occurs with the .cpy files. Interactive mode basically doing the entire process for each line. I highly doubt that there's a more efficient way to do so.
The iPython notebook works in a similar way.

Upvotes: 1

Related Questions