does lack of type declarations make Python unsafe?

Mark 'Kamikaze' Hughes kamikaze at
Mon Jun 30 01:39:58 CEST 2003

Sun, 29 Jun 2003 15:53:12 +0200, Anton Vredegoor <anton at>:
> kamikaze at (Mark 'Kamikaze' Hughes) wrote:
>>> And in neither C++ nor Java can you express "c is a container", either
>>> directly or indirectly, except in comments
>>  That is incorrect for Java, and for most C++ class libraries.
>>    List c = new ArrayList();
>>    c.add("foo");
>>    c.add("bar");
>>    System.out.println( c instanceof Collection );
>>    System.out.println( c.size() );
>>    for(Iterator it = c.iterator(); it.hasNext(); ) {
>>        System.out.println( );
>>    }
> As some googling seems to indicate this is an extendable list of
> references.

  Yes, Collections hold lists of Object references.

  You can see the javadoc, showing the inheritance tree and interfaces

> 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) );

  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.

  Retrieving the value is done by the ugly but standard idiom:
int n = ((Integer);

  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.

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

 <a href=""> Mark Hughes </a>
"We remain convinced that this is the best defensive posture to adopt in
order to minimize casualties when the Great Old Ones return from beyond
the stars to eat our brains." -Charlie Stross, _The Concrete Jungle_

More information about the Python-list mailing list