12.07.2015 Views

Is Python a

Is Python a

Is Python a

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

We’ve just run a <strong>Python</strong> script that prints a string and a number. We probably won’twin any programming awards with this code, but it’s enough to capture the basics ofprogram execution.<strong>Python</strong>’s ViewThe brief description in the prior section is fairly standard for scripting languages,and it’s usually all that most <strong>Python</strong> programmers need to know. You type code intotext files, and you run those files through the interpreter. Under the hood, though, abit more happens when you tell <strong>Python</strong> to “go.” Although knowledge of <strong>Python</strong>internals is not strictly required for <strong>Python</strong> programming, a basic understanding ofthe runtime structure of <strong>Python</strong> can help you grasp the bigger picture of programexecution.When you instruct <strong>Python</strong> to run your script, there are a few steps that <strong>Python</strong>carries out before your code actually starts crunching away. Specifically, it’s firstcompiled to something called “byte code” and then routed to something called a“virtual machine.”Byte code compilationInternally, and almost completely hidden from you, when you execute a program,<strong>Python</strong> first compiles your source code (the statements in your file) into a formatknown as byte code. Compilation is simply a translation step, and byte code is alower-level, platform-independent representation of your source code. Roughly,<strong>Python</strong> translates each of your source statements into a group of byte code instructionsby decomposing them into individual steps. This byte code translation isperformed to speed execution—byte code can be run much more quickly than theoriginal source code statements in your text file.You’ll notice that the prior paragraph said that this is almost completely hidden fromyou. If the <strong>Python</strong> process has write access on your machine, it will store the bytecode of your program in files that end with a .pyc extension (“.pyc” means compiled“.py” source). You will see these files show up on your computer after you’ve run afew programs alongside the corresponding source code files (that is, in the samedirectories).<strong>Python</strong> saves byte code like this as a startup speed optimization. The next time yourun your program, <strong>Python</strong> will load the .pyc and skip the compilation step, as long asyou haven’t changed your source code since the byte code was last saved. <strong>Python</strong>automatically checks the timestamps of source and byte code files to know when itmust recompile—if you resave your source code, byte code is automatically re-createdthe next time your program is run.Program Execution | 25

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!