Python compiled file generated even though wrong syntax
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Flying Over Ancient Lands
--
Chapters
00:00 Question
01:30 Accepted answer (Score 1)
03:47 Thank you
--
Full question
https://stackoverflow.com/questions/3947...
Accepted answer links:
[bytecode]: http://akaptur.com/blog/2013/11/17/intro.../
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #python27 #compilation
#avk47
ACCEPTED ANSWER
Score 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..