java introduction for pythonistas

Delaney, Timothy tdelaney at avaya.com
Tue Jan 28 19:05:03 EST 2003


> From: Alex Polite [mailto:m2 at plusseven.com]
> 
> java") I'm more interested in OO design patterns that Java supports (I
> know that it supports interfaces for one thing) that are not readily
> available in python, and other important differences. Is it possible
> to define new classes, functions, methods in Java? Are there 
> even unbound

One thing you will find is that a lot of patterns that Python supports
trivially are quite cumbersome in Java.

For example, the "Visitor" design pattern is trivial in Python - in fact, it
is fundamental to the language:

    for item in iterable:
        item.visit()  # or appropriate method

OR

    [ item.visit() for item in iterable ]

OR

    map(lambda item: item.visit(), iterable)

In java, each of these correspond to something like:

    Iterator iter = iterable.iterator();

    while (iter.hasNext())
    {
        ObjectSubclass item = (ObjectSubclass) iter.next();
        item.visit();
    }

Technically, the latter two correspond to:

    Iterator iter = iterable.iterator();
    List results = new ArrayList()

    while (iter.hasNext())
    {
        ObjectSubclass item = (ObjectSubclass) iter.next();
        results.append(item.visit());
    }

There are plenty of other cases like this.

Tim Delaney





More information about the Python-list mailing list