Chapter 9. User Classes

Table of Contents
Motivation
Theory: User-defined classes
The make_eiffel_stub.py script
Example: Using the Python smtplib.SMTP class to send mail from within Eiffel
Using an Eiffel subclass and a Python code generation script to make Eiffel shadow classes for arbitrary Python classes

Using shadow classes for Eiffel primitives and executing arbitrary code strings in the Python interpreter is all well and good, but does not address the real power of embedded Python--the ability to define and use new Python classes from within Eiffel. However, implementation of such features is not as straightforward as it would seem, and required a new base class and a Python code generation script to make it work.

Motivation

The real purpose for embedding Python in Eiffel is twofold: first, to allow the use of dynamic extensions to code; since Eiffel is statically typed and compiled, it is difficult to extend or change the run-time behaviour of an Eiffel program. An embedded Python layer would allow some objects to be implemented in Python and loaded at run-time, for example enabling a database package to be scripted like Microsoft's Access, or a CASE tool to implement loadable code generation modules. Second, one of Eiffel's weaknesses is its lack of a robust, full-featured standard library, one of the things which Python has in spades.

Both of these desired features require an ability that our library lacks so far; while primitives allow us to play with some basic Python interaction, they do not allow for extendibility or use of existing Python classes. The simple interaction with the interpreter could be jury-rigged to do this, but that solution lacks elegance.

What we need, then, is some mechanism for using arbitrary Python classes from within Eiffel. But how to go about it?