does lack of type declarations make Python unsafe?

Mark 'Kamikaze' Hughes kamikaze at kuoi.asui.uidaho.edu
Sun Jun 29 19:39:58 EDT 2003


Sun, 29 Jun 2003 15:53:12 +0200, Anton Vredegoor <anton at vredegoor.doge.nl>:
> kamikaze at kuoi.asui.uidaho.edu (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( it.next() );
>>    }
> 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
at:
<http://java.sun.com/j2se/1.4.1/docs/api/java/util/ArrayList.html>

> 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)it.next()).intValue();

  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="http://kuoi.asui.uidaho.edu/~kamikaze/"> 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