does lack of type declarations make Python unsafe?

Anton Vredegoor anton at vredegoor.doge.nl
Tue Jul 1 06:30:58 EDT 2003


kamikaze at kuoi.asui.uidaho.edu (Mark 'Kamikaze' Hughes) wrote:

<discussing Java collections>
>> Since int doesn't derive from object, this will probably
>> fail:
>> c.add(1)
>
>  This is true, but it's completely unimportant.  It's annoying at
>times, but it's an optimization for efficiency; there are good reasons
>why Java is several times faster than Python at most tasks.  You can use
>the primitive wrapper classes:
>c.add( new Integer(1) );

Interesting, first define it as unimportant and next focus un Java's
strong points.

>  On the rare occasions that I have to deal with a lot of Integer
>objects, I use a cache of the most commonly-used ranges, but Java's
>object creation is well-optimized these days, so there's no great harm
>in just newing up an Integer.

Weaken the use case and insist that it can be done with little extra
trouble, if any remaining -unbelievably persistent- user still would
think there's a need for it.

>  Retrieving the value is done by the ugly but standard idiom:
>int n = ((Integer)it.next()).intValue();

Show how ugly it would be.

>  Or you can write your own int container class, if you need to do that
>kind of operation frequently.  I find that it rarely comes up, because
>Java's normally used at a higher level.

Associate it with uncommon low level activities.

>  Java's not *pretty*, but it's a powerful and expressive language.

Expose the requested feature as a tradeoff with performance and
expressiveness.

Reading all this I get a very warped feeling of deja vu, because these
kind of techniques are often used here to fend off "attacks" against
Python. 

Maybe the "complete reversal" technique is new so I'll try it out:

"does lack of type declarations make Python unsafe?"

No it makes it safer, because being able to produce code that is
easier to read improves the validity of the code. (Validity is defined
here by "Does the program do what you think it does")

Since languages using type declarations don't enable the coder to "fly
by wire" and force her into all kinds of ugly code constructs, changes
are the coder will get lost in the code labyrinth. Therefore for such
languages extreme caution and large amounts of tests are unavoidable.
Of course these efforts use valuable coder time that would be better
employed in "algorithm space" instead of in "implementation space".

Anton




More information about the Python-list mailing list