[Tutor] Complied Python program...
Alan Gauld
alan.gauld at btinternet.com
Sun Jan 31 17:46:49 CET 2010
"Ken G." <beachkid at insightbb.com> wrote
> In using Geany, I have the ability to complied a Python program. For
> example, I can complied "program.py" to program.pyc."
You compile (note spelling) a python script everytime you import it
(if it has not already been compiled).
If you compile a new(or changed) script Python first compiles the
source text into an intermediate version called byte code (sometimes
called p-code) that the interpreter can understand and execute. It
saves the byte-code as a .pyc file so that in future it can import
the compiled version directly (if there is no newer .py file).
This byte-code is similar to what Java produces when you comile jave
into a class file. The Jave runtime engine is basically an interpreter
for Java byte-code. .Net code follows a similar approach too.
> What is the purpose of a complied Python program? Can it be transported
> to another computer without Python installed as run as it is?
No, it needs Python to run it but it can be transported without
the .py file existing - which gives a minimal amount of code
hiding and a marginal startup advantage the first time you run it.
Its not usually worth it IMHO.
You can see the p-code if you use the dis module. The docs give this
example:
>>> def myfunc(alist):
... return len(alist)
...>>> dis.dis(myfunc)
2 0 LOAD_GLOBAL 0 (len)
3 LOAD_FAST 0 (alist)
6 CALL_FUNCTION 1
9 RETURN_VALUE
The bytecode : 0,0,3,0,6,1,9 is more compact but much more primitive than
the original source text. It is similasr in character to a machine code
program but is machine independant.Thus it is said to run on a virtual
machine - the Python interpreter.HTH,-- Alan GauldAuthor of the Learn to
Program web sitehttp://www.alan-g.me.uk/
More information about the Tutor
mailing list