Using the interactive interpreter

When you download IronPython,33 you have two choices. You can download and install the msi installer (IronPython 2 only), which includes the Python 2.5 standard library. Alternately, you can download and unpack the binary distribution that comes as a zip file. Whichever route you take, you'll have two executables, ipy.exe and ipyw.exe, which are the equivalents of the Python executables python.exe and pythonw.exe. Both are used to launch Python scripts; ipy.exe launches them with a console window, and ipyw.exe launches programs as Windows applications (without a console).

ipy.exe path_to\python_script.py

If you run ipy.exe on its own, it starts an interactive interpreter session. The Iron-Python interpreter supports tab completion and coloring of the output, both of which are useful. The command line options to enable these are ipy -D -X:TabCompletion -X:ColorfulConsole

You should see something like figure 1.8.

Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp.

_____________ . D -X:TabCompletion -X:ColorfulConsole

Copyright (c) Microsoft Corporation, All rights reserved,

Figure 1.8 The IronPython interactive interpreter

From the IronPython website on CodePlex: http://www.codeplex.com/IronPython.

IronPython is dependent on the .NET framework 2.0.34 The interpreter, and any applications created with IronPython, will only run on computers with .NET 2.0 (or Mono) installed. In recent days, Microsoft has been pushing .NET 2.0 out to Windows machines via Windows update. A high proportion of Windows machines will already have .NET 2.0 installed. Windows machine that aren't .NET equipped will require at least the redistributable dotnetfx.exe.35

The interactive interpreter allows you to execute Python statements and see the results. This is known as a 'read-eval-print' loop (REPL).36 It can be extremely useful for exploring objects, trying out language features, or even performing quick one-off operations.

If you enter an expression, the resulting value will be printed to the console. The result of the last expression, whether value or object, is available in the interpreter by using the underscore (_).If you can't find a calculator, then you can always turn to the Python interpreter instead.

Code examples to be typed into an interactive interpreter session start each line that you enter with >>> or .... This is the interpreter prompt and reflects the actual appearance of the session. It's a common convention when presenting Python examples.

100.0 >>> x = _ >>> print x 100.0

More importantly, blocks of code can be entered into the interpreter, using indentation in the normal Python manner.

>>> def CheckNumberType(someNumber): ... if type (someNumber) == int: . . . print 'Yup, that was an integer'

... numType = type(someNumber)

>>> type(CheckNumberType)

We're not going to get very far in this book without building an understanding of .NET terminology. Before demonstrating some more practical uses of the interpreter, we'll look at some basic .NET concepts.

34 IronPython 2 requires .NET 2.0 Service Pack 1.

35 From the memorable URL: http://www.microsoft.com/downloads/details.aspx?FamilyID=0856eacb-4362-4b0d-8edd-aab15c5e04f5&displaylang=en.

36 From the first appearance of an interactive interpreter with the Lisp language.

0 0

Post a comment

  • Receive news updates via email from this site