dlp96
dlp96

Reputation: 151

How does python interpreter run the code line by line in the following code?

I have read that the interpreter runs the code line by line and reports the error if any at the same time and stops the further execution. So in python, consider the file ex1.py,

print "Hello world"
12variable = 'bye'
print 12variable

Now according to the working of interpreter, the interpreter would run the first line i.e. it prints hello world first and then show the syntax error in the next line (line-by-line working). Hence the expected output is:

Hello world
12variable = 'bye'
         ^
SyntaxError: invalid syntax

But the actual output is -

12variable = 'bye'
         ^
SyntaxError: invalid syntax

Why it is not printing Hello World at the first?

Upvotes: 11

Views: 7531

Answers (5)

Manikanta
Manikanta

Reputation: 11

Step 1:

The interpreter reads a python code or instruction. Then it verifies that the instruction is well-formatted, i.e. it checks the syntax of each line. If it encounters an error, it immediately halts the translation and shows an error message.

Step 2:

If there is no error, i.e. if the python instruction or code is well-formatted then the interpreter translates it into its equivalent form in an intermediate language called “Byte code”.Thus, after the successful execution of Python script or code, it is completely translated into Byte code.

Step 3:

Byte code is sent to the Python Virtual Machine(PVM).Here again, the byte code is executed on PVM. If an error occurs during this execution then the execution is halted with an error message. So in your case, the "invalid syntax" error is thrown because of step1. But, the actual print function gets executed at step 3. step 3 comes only after step 1 right... I think got it now.

Upvotes: 0

Morfidon
Morfidon

Reputation: 1528

I understand it that way:

Python runs the code line by line after it's in byte code state.

The difference between this thing and compilation (in other languages like C++) is that you have to do this process of interpretation each time you run the script.

Python interpreter interprets the code each time you run the script.

In C++ you compile the program and you can execute it without having to compile it again unless you want to change the system.

Upvotes: 0

Serge Ballesta
Serge Ballesta

Reputation: 148910

It depends on how you run the Python interpréter. If you give it a full source file, it will first parse the whole file and convert it to bytecode before execution any instruction. But if you feed it line by line, it will parse and execute the code bloc by bloc:

  • python script.py : parse full file
  • python < script.py : parse and execute by bloc

The latter is typically the way you use it interactively or through a GUI shell like idle.

Upvotes: 10

jwpfox
jwpfox

Reputation: 5232

Because your understanding of the interpreter is faulty. While it is possible for the behaviour you are describing to occur for a subset of errors it is not the common case for many (most?) errors.

If the interpreter can construct what it thinks is a valid program but there is an error at run time then what you are describing will happen.

Since the case you are pointing at is a syntax error that prevents a valid program being constructed the behaviour is as you see it.

Upvotes: 2

Trevor Merrifield
Trevor Merrifield

Reputation: 4691

It's a myth that Python is a fully interpreted language. When CPython runs a script the source code is parsed (this is where it will catch syntax errors), and compiled into bytecode (sometimes these are cached in your directory as .pyc files) before anything is executed. In this regard Python is not all that fundamentally different than Java or C# other than that it doesn't spend much time doing any optimizations, and I believe the bytecode is interpreted one instruction at a time, instead of being JITed to machine code (unless you're using something like PyPy).

Upvotes: 6

Related Questions