does lack of type declarations make Python unsafe?
Duncan Booth
duncan at NOSPAMrcp.co.uk
Tue Jun 17 09:14:11 EDT 2003
beliavsky at aol.com wrote in
news:3064b51d.0306170436.2b31f9e at posting.google.com:
> 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.)
You don't need to check that xx is a 1-D array since it is a result, so
your function just generates a 1-D array and returns it. Likewise you don't
need to check the type of rmse, and I really hope that in Python ierr would
disappear entirely in favour of an exception.
So you are down to the question of whether aa is a 2-D array, and whether
bb is a 1-D array of the correct size. Rather than checking the type of aa,
you could just try using it as a 2-D array. You will be pretty hard pushed
to accidentally pass in something of the wrong type that produces a result
rather than an exception.
I think you also need to distinguish not just between compile time and
runtime, but between compile time, test time and runtime. Compile and test
times happen (I hope) every few minutes while you are developing the
program. If you are passing an incompatible type into a function, this
should be caught at test time, i.e. within about 2 minutes maximum of you
writing the call to the function.
--
Duncan Booth duncan at rcp.co.uk
int month(char *p){return(124864/((p[0]+p[1]-p[2]&0x1f)+1)%12)["\5\x8\3"
"\6\7\xb\1\x9\xa\2\0\4"];} // Who said my code was obscure?
More information about the Python-list
mailing list