does lack of type declarations make Python unsafe?

Zac Jensen listbox at cybereal.org
Sun Jun 15 17:32:31 EDT 2003


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Sunday 15 June 2003 02:28 pm, beliavsky at aol.com wrote:
> 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.
>
> 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, 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.
>
> 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?
>
> 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.

I doubt you should worry.  It is not difficult at all to test for types when 
you feel it is necessary.

i.e. in the attached python script I give you an example of how to do explicit 
type checking when you need to do it.  This was test to work with python 
2.2.3

This is just one way to do it, I may not have even come close to the fanciest, 
or simplest way of checking, but, this is how I do it if it becomes an issue.

Another way I suppose would be through assert calls.. but they would be less 
informative than individual typeerror exceptions for each variable you 
check... (imho)

However, in many cases you can coerce or convert types, or in the case of 
complex classes, you can use properties that do the type checking and raise 
exceptions.

Usually it's not a problem for python programmers, or this would have been 
addressed with a different solution a long long time ago.

hope that helps.

- -Zac
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.2 (GNU/Linux)

iD8DBQE+7OXvV3L7YsSif1URAoQiAJ98+x593lskvHAezpZxHoRTiIwBQgCbBYH7
yLPtXTfnB3EYzTqkBH7zJLg=
=UmuZ
-----END PGP SIGNATURE-----
-------------- next part --------------
A non-text attachment was scrubbed...
Name: testd.py
Type: text/x-python
Size: 608 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20030615/a5e13c01/attachment.py>


More information about the Python-list mailing list