DekGenius.com
[ Team LiB ] Previous Section Next Section

27.7 Executing Programs

The last set of built-in functions in this section have to do with creating, manipulating, and calling Python code. See Table 27-13.

Table 27-13. Ways to execute Python code

Name

Behavior

import

Executes the code in a module as part of the importing and binds it to a name in the scope in which it is executed. You can choose what name is chosen by using the import modulename as name form.

exec code [ in globaldict [, localdict]]

Executes the specified code (string, file, or compiled code object) in the optionally specified global and local namespaces. This is sometimes useful when reading programs from user-entered code as in an interactive shell or "macro" window.

compile(string, filename, kind)

Compiles the string into a code object. This function is only useful as an optimization.

execfile(filename[, globaldict[, localdict]])

Executes the program in the specified filename, using the optionally specified global and local namespaces. This function is sometimes useful in systems which use Python as an extension language for the users of the system.

eval(code[, globaldict[, localdict]])

Evaluates the specified expression (string or compiled code object) in the optionally specified global and local namespaces, and returns the expression's result. Calculator-type programs that ask the users for an expression to compute often use eval.

It's a simple matter to write programs that run other programs. Shortly, we'll talk about ways to call any program from within a Python program. There are several mechanisms that let you execute arbitrary Python code. The most important is one statement we've used throughout the book, import, which executes code existing in files on the Python path. There are several other ways to execute Python code in-process. The first is the exec statement:

exec code [ in globaldict [, localdict]]

exec takes between one and three arguments. The first argument must contain Python code—either in a string, as in the following example; in an open file object; or in a compiled code object. For example:

>>> code = "x = 'Something'"
>>> x = "Nothing"                            # Sets the value of x
>>> exec code                                # Modifies the value of x!
>>> print x
'Something'

exec can take optional arguments. If a single dictionary argument is provided (after the then-mandatory in word), it's used as both the local and global namespaces for the execution of the specified code. If two dictionary arguments are provided, they are used as the global and local namespaces, respectively. If both arguments are omitted, as in the previous example, the current global and local namespaces are used.

When exec is called, Python needs to parse the code that is being executed. This can be a computationally expensive process, especially if a large piece of code needs to be executed thousands of times. If this is the case, it's worth compiling the code first (once), and executing it as many times as needed. The compile function takes a string containing the Python code and returns a compiled code object, which can then be processed efficiently by the exec statement.

compile takes three arguments. The first is the code string. The second is the filename corresponding to the Python source file (or '<string>' if it wasn't read from a file); it's used in the traceback in case an exception is generated when executing the code. The third argument is one of 'single', 'exec', or 'eval', depending on whether the code is a single statement whose result would be printed (just as in the interactive interpreter), a set of statements, or an expression (creating a compiled code object for use by the eval function).

A related function is the execfile built-in function. Its first argument must be the filename of a Python script instead of a file object or string (remember that file objects are the things the open built-in returns when it's passed a filename). Thus, if you want your Python script to start by running its arguments as Python scripts, you can do something like:

import sys
for argument in sys.argv[1:]:          # We'll skip ourselves, or it'll go forever!
    execfile(argument)                 # Do whatever.

Two warnings are warranted with respect to execfile. First, it is logical but sometimes surprising that execfile executes by default in its local scope—thus, calling execfile from inside a function will often have much more localized effects than users expect. Second, execfile is almost never the right answer—if you're writing the code being executed, you should really put it in a module and import it. The behavior you'll get will be much more predictable, safe, and maintainable—it's just too easy for execfile( ) code to wreak havoc with the module calling execfile.

Two more functions can execute Python code. The first is the eval function, which takes a code string (and the by now expected optional pair of dictionaries) or a compiled code object and returns the evaluation of that expression. For example:

>>> word = 'xo'
>>> z = eval("word*10")
>>> print z
'xoxoxoxoxoxoxoxoxoxo'

The eval function can't work with statements, as shown in the following example, because expressions and statements are different syntactically:

>>> z = eval("x = 3")
Traceback (innermost last):
File "<stdin>", line 1, in ?
File "<string>", line 1
x = 3
      ^
SyntaxError: invalid syntax

The last function that executes code is apply. It's called with a callable object, an optional tuple of the positional arguments, and an optional dictionary of the keywords arguments. A callable object is any function (standard functions, methods, etc.), any class object (they create an instance when called), or any instance of a class that defines a __call__ method. apply is slowly being deprecated because it is no longer necessary or the most efficient way of doing what it does—you can always replace it by simple calls, optionally using the * and ** argument markers. As we've seen in the chapter on OOP, one can call a method defined in a base class using:

class Derived(Base):
    def __init__(self, arg, *args, **kw):
        self.__init__(self, *args, **kw)

This code used to be written using apply as in:

class Derived(Base):
    def __init__(self, arg, *args, **kw):
        apply(self.__init__, (self,) + args), kw)

As you can see, the new variant is much cleaner, which is why apply is quickly becoming obsolete.

If you're not sure if an object is callable (e.g., if it's an argument to a function), test it using the callable built-in, which returns true if the object it's called with is callable.[7]

[7] You can find many things about callable objects, such as how many arguments they expect and what the names and default values of their arguments are by checking the Language Reference for details, especially Section 3. 2, which describes all attributes for each type. Even easier is using the inspect module, which is designed for that.

>>> callable(sys.exit), type(sys.exit)
(1, <type 'builtin_function_or_method'>)
>>> callable(sys.version), type(sys.version)
(0, <type 'string'>)

There are other built-in functions we haven't covered; if you're curious, check a reference source such as the library reference manual (Section 2.3).

    [ Team LiB ] Previous Section Next Section