Does python always need to compile ENTIRE program before it can start to run it???

  • Thread starter Christian Seberino
  • Start date
C

Christian Seberino

I can achieve something similar to Python's automatic compilation
in C/C++ by making a script that compiles and runs my program like this:


make ; myprogram


I am trying to think of an advantage Python has over this hack....

Is it true that unlike C/C++ that Python programs can start executing
before compilation is COMPLETELY done??? I think so but I'm not sure.

Chris
 
K

KefX

I can achieve something similar to Python's automatic compilation
in C/C++ by making a script that compiles and runs my program like this:


make ; myprogram


I am trying to think of an advantage Python has over this hack....

Is it true that unlike C/C++ that Python programs can start executing
before compilation is COMPLETELY done??? I think so but I'm not sure.

Chris

Python compiles to bytecode rather than native code, so I'm not sure this
analogy really applies...also modules are only compiled when imported; if you
execute a script directly, a .pyc file will not be emitted, but it will still
be compiled in RAM.

- Kef
 
J

John Roth

Christian Seberino said:
I can achieve something similar to Python's automatic compilation
in C/C++ by making a script that compiles and runs my program like this:


make ; myprogram


I am trying to think of an advantage Python has over this hack....

Is it true that unlike C/C++ that Python programs can start executing
before compilation is COMPLETELY done??? I think so but I'm not sure.

Since Python is a compiled language, yes, the programs have
to be compiled before they run. However, Python saves the
compiled version of all modules *except* the top level script,
so it doesn't have to be redone on each execution. I've never
gotten a satisfactory explanation of why it doesn't save the compiled
version of the top level script, but then, I've never really looked
into it. It's just curious.

Compilation must be finished before the import statement can complete.

John Roth
 
P

Peter Hansen

Christian said:
I can achieve something similar to Python's automatic compilation
in C/C++ by making a script that compiles and runs my program like this:

make ; myprogram

I am trying to think of an advantage Python has over this hack....

Is it true that unlike C/C++ that Python programs can start executing
before compilation is COMPLETELY done??? I think so but I'm not sure.

Yes, this is true. Assuming (as would be the case in any serious
program) your code is broken into appropriate modules, only the first
module loaded is compiled prior to its execution. Other modules are
compiled on an as-needed basis, as they are imported for the first
time. Also note that Python has automatic caching of the compiled
code in a .pyc file, and recompilation occurs only if the timestamp
of the source .py file is different than that stored in the .pyc file.

In essence, the above "hack", as you so aptly call it, has *no*
advantages over Python's approach, and Python makes it all transparent
anyway so you don't even need to worry about it.

-Peter
 
R

Rene Pijlman

Christian Seberino:
I can achieve something similar to Python's automatic compilation
in C/C++ by making a script that compiles and runs my program like this:

make ; myprogram

From what I remember when I programmed in C, this would start a recursive
include file dependency check that, 10 minutes later, usually decided to
recompile the whole tree.
 
J

James Kew

I've never
gotten a satisfactory explanation of why it doesn't save the compiled
version of the top level script, but then, I've never really looked
into it. It's just curious.

I've always wondered that too. The top-level script _might_ be standard
input or command-line, but it seems to me that it often (usually?) is a .py
which _could_ be stashed into a .pyc.

Taken to extremes, it suggests that top-level scripts should be as small as
possible: an if __name__ == "__main__" delegating immediately to a worker
function imported from a helper module. I'm not sure there's anything
realistic that's code-heavy and short-running enough for it to make much
difference, though...

James
 
P

Piet van Oostrum

JK> I've always wondered that too. The top-level script _might_ be standard
JK> input or command-line, but it seems to me that it often (usually?) is a .py
JK> which _could_ be stashed into a .pyc.

And then? Next time you call again python script.py, not script.pyc.
That's the difference with import: import says `import script', not `import
script.py'.
 

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,743
Messages
2,569,477
Members
44,898
Latest member
BlairH7607

Latest Threads

Top