Does Python compete with Java?

Roy Smith roy at panix.com
Mon Apr 5 21:02:24 EDT 2004


"John Roth" <newsgroups at jhrothjr.com> wrote:
> recently some of the industry gurus have been questioning whether the 
> additional complexity of static typing and other Java complexities is 
> paying its weight as compared to Python, especially when used in a 
> TDD type development environment.

Yeah, tell me about it.  I've been playing around with some JSP stuff 
lately.  Here's a method I wrote:

    protected void doLogin (HttpServletRequest request,
                            HttpServletResponse response)
        throws ServletException
    {
        String name = (String) request.getParameter ("name");
        String password = (String) request.getParameter ("password");
        Connection connection = getDbConnection (request);

        try {
            LoginModel model = new LoginModel (connection);
            boolean loginIsValid = model.validate (name, password);

            if (loginIsValid) {
                makeMainPage (request);
                forward (request, response, "/main.jsp");
            } else {
                forward (request, response, "/badLogin.jsp");
            }
        }
        catch (SQLException exception) {
            throw new ServletException (exception);
        }
        catch (IOException exception) {
            throw new ServletException (exception);
        }
    }

It's just filled with extraneous crap that's only there to make the java  
complier happy and has nothing to do with the logic of my application.  
The type declarations and casts are just the beginning.  The interface 
I'm inheriting from requires that the only exception I throw be 
ServletException, so I need to catch all the others are re-throw.  
Here's what the same logic would look like in Python, as a mechanical 
transliteration:

    def doLogin (request, response):
        name = request.getParameter ("name");
        password = request.getParameter ("password");
        connection = getDbConnection (request);

        model = LoginModel (connection);
        loginIsValid = model.validate (name, password);

        if loginIsValid:
            makeMainPage (request);
            forward (request, response, "/main.jsp");
         else:
            forward (request, response, "/badLogin.jsp");
 
13 lines of code instead of the original 26!  This 2:1 ratio seems to be 
pretty typical in my experience.  It's not that I'm cramming more 
application logic onto each line in the Python version, it's that I'm 
getting rid of the fluff that takes up lines without adding anything 
useful.

The end result is that it's harder to write, and the effort that goes 
into making the compiler happy is that much less effort that I can put 
into making sure I really understand how my application should be 
designed, and testing it.  It's a seeing the forest for the trees kind 
of issue.

I think it's also a lot easier to read and understand the Python 
version.  Pretty much every line maps directly to the application logic, 
with very little overhead.

Found on the web (http://www-users.cs.york.ac.uk/~susan/joke/foot.htm)...

> How to Shoot Yourself In the Foot
> [....]
> Java
> You locate the Gun class, but discover that the Bullet  class is abstract, so 
> you extend it and write the missing part of the  implementation. Then you 
> implement the ShootAble  interface for your foot, and recompile the Foot 
> class. The interface lets the bullet call the doDamage method on the  Foot, 
> so the Foot can damage itself in the  most effective way. Now you run the 
> program, and call the doShoot  method on the instance of the Gun class. First 
> the Gun  creates an instance of Bullet, which calls the doFire  method on the 
> Gun. The Gun calls the hit(Bullet)  method on the Foot, and the instance of 
> Bullet  is passed to the Foot. But this causes an IllegalHitByBullet  
> exception to be thrown, and you die.

They don't have one for Python, but I expect it would be something like 
this:

You create a Foot object and call its shoot() method.  The bullet makes 
a hole in your foot, but by that time you've gotten dragged into a huge 
flamewar about the relative merits of spaces vs. tabs for indentation 
and barely notice the pain.



More information about the Python-list mailing list