Flylib.com

Books Software

 
 
 

Section 1.8. Other Implementations


1.8. Other Implementations

The "standard" version of Python is C-compiled, aka CPython. There are a few other Python implementations. We will describe some here, but for more on the various Python implementations out there, check out:

http://python.org/dev/implementations.html

Java

As we mentioned in the previous section, a Python interpreter completely written in Java, called Jython, is currently available. Although there are still minor differences between the two interpreters, they are very similar and provide a comparable startup environment.

What are the advantages of Jython? Jython ...

  • Can run anywhere a Java virtual machine (JVM) can be found

  • Provides access to Java packages and class libraries

  • Furnishes a scripting environment for Java development

  • Enables ease of testing for Java class libraries

  • Offers access to Java's native exception handling ability

  • Delivers JavaBeans property and introspection ability

  • Encourages Python-to-Java development (and vice versa)

  • Gives GUI developers access to Java AWT/Swing libraries

  • Utilizes Java's native garbage collector (so CPython's was not implemented)

    A full treatment of Jython is beyond the scope of this text, but there is a good amount of information online. Jython is still an ongoing development project, so keep an eye out for new features. You can get more information at the Jython Web site at:

    http://jython.org

.NET/Mono

There is now a Python implementation completely in C#, called IronPython. It is targeted at the .NET and Mono environments. You can integrate an IronPython interpreter in a .NET application that can interact with .NET objects. Extensions to IronPython can be implemented in C# or VisualBasic.NET. In addition, there is another .NET/Mono language that is Python-inspired, and it is called Boo. You can find out more information about IronPython and Boo at:

http://codeplex.com/Wiki/View.aspx?ProjectName=IronPython

http://boo.codehaus.org/

Stackless

One of the limitations of CPython is that for each Python function call, it results in a C function call. (For the computer science-oriented, we are talking about stack frames here.) This implies restrictions on CPython, most notably a limitation on the total number of concurrent function calls. This can make it difficult to implement effective user -level threading libraries or highly recursive applications in Python. If this total is exceeded, then your program will crash. By using a "stackless" implementation, you are freed from this restriction and can have any number of Python stack frames for the one C stack frame. This allows you to have many function calls and supports a very large number of threads. The main stackless implementation of Python is called ... Stackless (surprise!).

The only problem with Stackless is that it requires significant changes to the existing CPython interpreter, so it is seen as an independent fork. Another project called Greenlets that also supports microthreads is available as a standard C extension and can be used with an unmodified version of Python. You can read about both of these projects at:

http://stackless.com

http://codespeak.net/py/current/doc/greenlet.html



1.9. Exercises

1-1.

Python Installation . Check if Python is installed on your system. If not, download and install it!

1-2.

Executing Python . How many different ways are there to run Python? Which do you prefer and why?

1-3.

Python Standard Library .

  1. Find where the Python executables and standard library modules are installed on your system.

  2. Take a look at some of the standard library files, for example, string.py . It will help you get acclimated to looking at Python scripts.

1-4.

Interactive Execution. Start your Python interactive interpreter. You can invoke it by typing in its full pathname or just its name ( python or python.exe ) if you have installed its location in your search path . (You can use any version or implementation of Python that is convenient to you, e.g., command line, GUI/IDE, Jython, IronPython, or Stackless.) The startup screen should look like the ones depicted in this chapter. When you see the >>> , that means the interpreter is ready to accept your Python commands.

Try entering the command for the famous Hello World! program by typing print 'Hello World!' (and press RETURN), then exit the interpreter. On Unix systems, ˆD will send the EOF signal to terminate the Python interpreter, and on DOS systems, the keypress is ˆZ. Exiting from windows in graphical user environments like the Macintosh, PythonWin or IDLE on Windows , or IDLE on Unix can be accomplished by simply closing their respective windows .

1-5.

Scripting . As a follow-up to Exercise 1-4, create "Hello World!" as a Python script that does exactly the same thing as the interactive exercise above. If you are using the Unix system, try setting up the automatic startup line so that you can run the program without invoking the Python interpreter.

1-6.

Scripting . Create a script that displays your name, age, favorite color , and a bit about you (background, interests, hobbies, etc.) to the screen using the print statement.