does lack of type declarations make Python unsafe?

beliavsky at aol.com beliavsky at aol.com
Tue Jun 17 08:36:55 EDT 2003


David Abrahams <dave at boost-consulting.com> wrote in message news:<uwufmko7g.fsf at boost-consulting.com>...

SNIP

> I find that static typing makes a big difference for two things:
> 
>   1. Readability.  It really helps to have names introduced with a
>      type or a type constraint which expresses what kind of thing they
>      are.  This is especially true when I am coming back to code after
>      a long time or reading someone else's work.  Attaching that
>      information to the name directly is odious, though, and leads to
>      abominations like hungarian notation.

I strongly agree with this. For numerical work, especially numerical
linear algebra, having the compiler check types and the # of
dimensions can detect many errors.

The beginning of the following Fortran 95 subroutine, for example,
provides useful information both to the compiler and to a programmer
trying to understand what it does:

subroutine solve_lsq_svd_rmse(aa,bb,xx,rmse,ierr)
! solve a set of least-squares linear equations and compute the rmse
real     , intent(in)   :: aa(:,:) ! matrix of independent variables
real     , intent(in)   :: bb(:)   ! vector of dependent variable
real     , intent(out)  :: xx(:)   ! solution of least-squares problem
real     , intent(out)  :: rmse    ! rmse of regression
integer  , intent(out)  :: ierr    ! error flag

In Python, to ensure that aa is a 2-D array and that bb and xx are 1-D
arrays, you need to write some checking code, which will not be as
clear as the above declarations IMO. Even if you do this, the errors
will be caught at run time, not compile time.

(Regardless of the language, you need to check that the # of rows in
aa equals the # of elements in bb. This usually must be done at run
time.)




More information about the Python-list mailing list