Next: Bibliography
Up: Python's object model, with
Previous: Factoring generic code
Pedagogically, the HTMLgen problem is not only intellectually
exciting, it is very motivating, because it is perceived by students as
practical. The HTMLgen.py class library is well written and easy to
read. It is a significant piece of open source software that students
can benefit not only from using, but also from studying critically.
This paper shows that Java's and Python's underlying object models are
very different; it is useful for students to compare these differences
and see how they impact their solutions to problems such as
HTMLgen.
There are other differences between Java's and Python's object
orientation styles. Some examples include:
- In both languages the first
argument of an instance method must be a reference to the instance, in
Java this argument is implicit and must not be declared; in Python it
must be explicitly declared. This was briefly mentioned in Section 2.
- Java forces constructor chaining (often implicitly),
meaning that a constructor call is guaranteed to call the
constructors of all superclasses. In contrast, if a Python
constructor wishes to call its ancestors' constructors, it must do so
explicitly. This was demonstrated in Section 7. To wit, in line 28,
Href's __init__ needs to call
HTMLElement's __init__ explicitly.
The comparison of the implicit versus explicit instance reference and
constructor chaining deepens students' understanding of such concepts.
- The above example also shows that a Python constructor
is just a method (line 28), while in constrast, a Java constructor is
not.
- Java allows a class to subclass from
only one class and any number of interfaces; a Python class can subclass
from any number of classes. Python's multiple inheritance is easy to
understand and use.
- Finally, a Python class instance can dynamically
add or delete a method, instance variable or superclass; Java
cannot.
It is beyond this paper's scope to make an exhaustive
comparison.
Next: Bibliography
Up: Python's object model, with
Previous: Factoring generic code
Luby Liao
Sep 10, 2004