15.04.2013 Views

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

Core Python Programming (2nd Edition)

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 14. Execution Environment<br />

Chapter Topics<br />

● Callable Objects<br />

● Code Objects<br />

● Statements and Built-in Functions<br />

● Executing Other Programs<br />

● Terminating Execution<br />

● Miscellaneous Operating System Interface<br />

● Related Modules<br />

There are multiple ways in <strong>Python</strong> to run other pieces of code outside of the currently executing<br />

program, i.e., run an operating system command or another <strong>Python</strong> script, or execute a file on disk or<br />

across the network. It all depends on what you are trying to accomplish. Some specific execution<br />

scenarios could include:<br />

● Remain executing within our current script<br />

● Create and manage a subprocess<br />

● Execute an external command or program<br />

● Execute a command that requires input<br />

● Invoke a command across the network<br />

● Execute a command creating output that requires processing<br />

● Execute another <strong>Python</strong> script<br />

● Execute a set of dynamically generated <strong>Python</strong> statements<br />

● Import a <strong>Python</strong> module (and executing its top-level code)<br />

There are built-ins and external modules that can provide any of the functionality described above. The<br />

programmer must decide which tool to pick from the box based on the application that requires<br />

implementation. This chapter sketches a potpourri of many of the aspects of the execution environment<br />

within <strong>Python</strong>; however, we will not discuss how to start the <strong>Python</strong> interpreter or the different<br />

command-line options. Readers seeking information on invoking or starting the <strong>Python</strong> interpreter<br />

should review Chapter 2.<br />

Our tour of <strong>Python</strong>'s execution environment consists of looking at "callable" objects and following up<br />

with a lower-level peek at code objects. We will then take a look at what <strong>Python</strong> statements and built-in<br />

functions are available to support the functionality we desire. The ability to execute other programs<br />

gives our <strong>Python</strong> script even more power, as well as being a resource-saver because certainly it is<br />

illogical toreimplement all this code, not to mention the loss of time and manpower. <strong>Python</strong> provides<br />

many mechanisms to execute programs or commands external to the current script environment, and<br />

we will run through the most common options. Next, we give a brief overview of <strong>Python</strong>'s restricted<br />

execution environment, and finally, the different ways of terminating execution (other than letting a<br />

program run to completion). We begin our tour of <strong>Python</strong>'s execution environment by looking at<br />

"callable" objects.

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

Saved successfully!

Ooh no, something went wrong!