3

I wrote a small program 'test1.py'

print abc
print 'the above is invalid'

Now i write a different python program 'test2.py'

import test1
print 'this line will not get executed'

Q1:To my surprise,i can see test1.pyc file is successfully generated. Why? Since test1.py conatins an invalid statement in first line, why at all is test1.pyc file getting generated? What exactly does the compiler check(is it the syntax or something else?).I am getting confused.Please clarify.

Q2: I also read that the compiled python file will be further interpreted.Is that true?

Q3:Compiler converts the program to machine code as a whole.This does not need to be further interpreted via an interpreter? Is it true? If so,then what about Question2?

Q4:I also read that compiled code is closer to the machine. When we use an interpreter,it converts the code into intermediate code which needs to be further converted to machine code.Is that correct? So, compiled code is closer to the machine than interpreted code?

1 Answer 1

1

Q1:

Neither file contains any syntax errors. The module test1 can therefore successfully be compiled into instructions the interpreter can read. The *compiling however has no code introspection that can determine ahead of time whether a variable is defined at any given point or not.

*I like to think of this conversion more as translation than compilation, as it largely does not alter the code structure at all, but rather translates it into instructions that are easier for the interpreter to read. Compilation implies that code inspection is taking place beyond simple syntax checking. (Google translate will often give something grammatically correct, but it may or may not make any sense.

Q2:

The python interpreter understands what's called bytecode. It's functionally a program written in c that takes the *compiled code and executes it on the machine. For each variation of hardware you want to run your code on, a version of this program(specifically ceval.c) is compiled to work with that hardware (be it x86, arm, mips, etc...), and interprets the bytecode which is the same no matter what hardware you are running. This is what allows python (and many other interpreted languages) to be cross-platform

Q3

No this is not true. *compiled python code runs through the same interpreter normal code does. The benefit of *compiled python code is in loading time of modules. Before any python code is executed it is converted into bytecode then sent to the interpreter. With a script this is done each time, but when python imports a script as a module it saves a copy of the already parsed bytecode to save itself the trouble next time.

Q4

Your confusion here is likely due to the poor naming convention of python *compiled files. They are not truly compiled into machine instructions, so they must be executed by a program rather than on the hardware itself. True compilers are programs that translate and optimize code (c, c++, fortran etc..) and spit out actual binary hardware instructions as specified by the manufacturer.

I did my best to guess what you were confused about, but if you have any more questions feel free to ask..

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.