does lack of type declarations make Python unsafe?

John Roth johnroth at ameritech.net
Sun Jun 15 17:35:49 EDT 2003


<beliavsky at aol.com> wrote in message
news:3064b51d.0306151228.22c595e0 at posting.google.com...
> In Python, you don't declare the type of a variable, so AFAIK there is
> no way for the interpreter to check that you are calling functions
> with variables of the correct type.

A nit. Actually, the interpreter does find this out. It's the compiler
that's clueless.

> Thus, if I define a function correl(x,y) to compute the correlation of
> two vectors, which makes sense to me only if x and y are 1-D arrays of
> real numbers, Python will not stop me from trying to compute the
> correlation of two arrays of integers or strings. C++ and Fortran 95
> compilers can spot such errors.
>
> Calling functions with invalid arguments is one of the commonest
> programming errors,

I'm not sure I'd go that far. What's a common programming
error depends on the programmer, after all.

> and I think it is worthwhile to declare variables,
> especially the dummy arguments of functions, explicitly to avoid such
> errors, even if it takes a few more lines of code. I worry that
> Python's convenience in writing small programs comes at the cost of
> making bug-free large programs more difficult to write.

It depends on your programming style. If you're in the habit of
writing several hundred lines of code at a time, and then debugging it,
you're probably right - a compile error would be a lot more productive
than finding it out several hours into a debugging session.

On the other hand, if you're using an incremental program construction
process like XP's Test Driven Development, then it doesn't make any
difference at all. The interpreter will give you the stack trace a
couple
of seconds later than the compiler would be able to give you an error
message.

> I have only been programming in Python for a few weeks -- am I right
> to worry?
> Are there techniques to ensure that functions are called with
> appropriate arguments?

A lot of programmers like to use the "assert" statement for
additional checks on the arguements. Assert will be compiled out
if you specify optimization, so it doesn't have production performance
issues, and many people think that it helps in documenting what the
proper inputs are to a method.

Also, there's a lot of work going into a facility that will be
similar to Eiffel's "Design by Contract."

> When I actually try to call correl(x,y) in the program
>
> from stats import correl
> from Numeric import array
> a = array(["a","b"])
> b = array(["c","d"])
> print correl(a,b) # should be arrays of Floats, not strings
>
> I get the result
>
> Traceback (most recent call last):
>   File "xcorr.py", line 5, in ?
>     print correl(a,b)
>   File "stats.py", line 88, in correl
>     ax  = mean(x)
>   File "stats.py", line 57, in mean
>     return sum(x)/n
> TypeError: unsupported operand type(s) for /: 'str' and 'int'
>
> It's good that the program crashes rather than returning a bad result,
> but a C++ or Fortran 95 compiler would flag the bad arguments during
> compilation, which I think is still better. Also, in a large script,
> the bad call to correl(x,y) could come after much CPU time had
> elapsed, potentially wasting a lot of programmer's time.

Again, see my comment about incremental development. It's
much more important in a language like Python, although it will
undoubtedly help any language.

John Roth






More information about the Python-list mailing list