Python - interpreted vs compiled

C

codecraig

Hi,
I have a question about Python. I know that it is an interpreted
language, meaning a python program is converted to binary on the fly
each time it is run, or compiled.
What would be the purpose of compiling? I guess the compiled python
code (question, is compiled python code called byte code?..if not, what
is it called?) is not readable since it is not plain text, which may be
a reason for compiling...but why else??

Thanks.
 
F

Fredrik Lundh

codecraig said:
I have a question about Python. I know that it is an interpreted
language, meaning a python program is converted to binary on the fly
each time it is run, or compiled.
What would be the purpose of compiling? I guess the compiled python
code (question, is compiled python code called byte code?..if not, what
is it called?) is not readable since it is not plain text, which may be
a reason for compiling...but why else??

the Python runtime runs custom object code ("byte codes") on a virtual machine.
the compilation process converts source code to object code. to speed things up,
the object code (or byte code, if you prefer) is stored on disk, so it can be reused
the next time the program is run.

</F>
 
B

bruno modulix

codecraig said:
Hi,
I have a question about Python. I know that it is an interpreted
language,

Nope. A *language* is neither compiled nor interpreted. An
*implementation* of a language can use strict interpretation, byte-code
compilation + VM, or native code compilation (it could as well use
purely mechanical stuff or whatever).

CPython - the current reference implementation of Python - uses
byte-code compilation + VM. Jython too, but is relies on the Java
byte-code and VM. IronPython targets MS .NET CLR.
meaning a python program is converted to binary on the fly
each time it is run, or compiled.

Only the main script is recompiled each time - unless you compile it by
yourself. Imported code is (re)compiled only if necessary.
What would be the purpose of compiling?

Having an intermediate representation that is faster to execute for the
VM. What is the purpose of compiling, be it to byte-code or to native
object code ?
I guess the compiled python
code (question, is compiled python code called byte code?
yes.

is not readable since it is not plain text,

You mean human-readable ? Depends on which human reads it !-)
(NB : No, I can't read byte-code)
which may be
a reason for compiling...

No, this is an effect, not a cause.

Reverse-engineering Python byte-code is quite easy (idem for Java). This
is not meant to "protect" code. Neither is native object-code
compilation a protection against craking - or we wouldn't see so many
cracked applications on Windows machines...
but why else??

Execution speed. A byte-code is kind of an assembly language for a
non-existing processor (hence the 'virtual machine' concept). This is
exactly the same motivation as for native-code compilation : not having
to tokenise/parse/translate the code each time it's runned.

The main difference here between Python and Java is that the Python
runtime takes care of those details for you instead of requiring you to
either manually recompile everything each time you make a change or use
a complex build system (that you'll have to - manually - maintain anyway).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top