tags:

views:

62

answers:

5

Does python compile down to some byte code or is it rendered on the fly each time like php/asp?

From my readings I read it has its own byte code format, so i figured it was like java/.net where it compiles into a intermediate language/byte code.

so it is more effecient in that respect that php right?

A: 

You can compile python to PYC ( http://effbot.org/zone/python-compile.htm ). Just executing a python script, however, does not do this compilation automatically, so a CGI web setup with python will not automatically compile.

Martin Eve
A: 

When you use a module in Python, it (generally) gets compiled if it hasn't been already. For example, if you have a Django app deployed with just .py files, they'll get compiled (and output as .pyc files) as the modules are imported by the app.

mipadi
so it isn't as "effecient" as java/.net in this regard then?
Blankman
In what way? It's not compiled *before deployment*, if that's what you mean. (And in general, Python doesn't have the same performance as Java, but that's for other reasons.)
mipadi
+1  A: 

Given a language X, and a way the server can be aware of it (a module or whatever) or a proper "intermediate" CGI program mX, this mX can be programmed so that it indeed interprets directly plain text script in X (like php), or bytecode compiled code (originally written in X). So, provided the existance of the proper mX, it could be both options. But I think the most common one is the same as php and asp.

Coping with bytecodes can be more efficient than interpreting scripts (even though modern interpreters are not implemented in the simple way and use "tricks" to boost performance)

ShinTakezou
+1  A: 

You can easily see the bytecode:

>>> import dis
>>> dis.dis(lambda x: x*2)
  1           0 LOAD_FAST                0 (x)
              3 LOAD_CONST               0 (2)
              6 BINARY_MULTIPLY     
              7 RETURN_VALUE        
>>> 
Marco Mariani
A: 

Python modules are 'compiled' to .pyc files when they are imported. This isn't the same as compiling Java or .NET though, what's left has an almost 1-1 correspondence with your source; it means the file doesn't have to be parsed next time, but that's about all.

You can use the compile or compileall modules to pre-compile a bundle of scripts, or to compile a script which wouldn't otherwise be compiled. I don't think that running a script from the command line (or from CGI) would use the .pyc though.

Andrew Aylett