Python complaints

Charles Boncelet boncelet at udel.edu
Thu Dec 16 05:42:46 EST 1999


Previously, I suggested "I think all functions that operate 
on single things should be able to operate on a list of 
things and return a list of things. (Are there
obvious reasons why this paradigm can't work?)"

And many people rightly beat me up on this (especially on my
suggestion that "len" should change--I didn't really mean it
should undergo a non-backward-compatible change, but my argument
wasn't worded as precisely as it should have been.) 

However, let me clarify further (as I have only been using Python
for a few months, my thoughts and impressions are still forming):

I am having a hard time figuring out the philosophy behind types
and typing in Python.  Even though I can't declare the type returned
by a function or the type of its arguments, Python is very picky 
about the types involved.

If I call a function with the wrong type arguments, I (usually) get
a failure of one sort or another. E.g., len(5), math.sin([1,2,3]) 
both fail, although I think both should succeed. Numeric.sin([1,2,3]) 
succeeds.

However there is nothing in the function declaration that specifies
the type of its arguments.  In many cases, the way one finds out
what types are allowed is by experimentation: call the function and
see if you get an error.

E.g., the doc string for math.sin is "sin(x) return the sine of x".
Nowhere does it tell me that "x" should be a number, not a list.

What kind of number is allowed?  Can x be an integer, a float, complex,
long, hex?  There is no way to know, without doing the experiment.
BTW, experimentation indicates that it fails if x is complex and works
for the others. sin seems to be promoting its argument to a float and
taking the sine.  However, sin(complex) is a perfectly good mathematical
operation.  It should work.

(I am using math.sin(x) as an example of the confusion, not because I 
need this specific function.  Numeric.sin(x) is clearly 
better written than math.sin(x).)

If Python is a typeless language, then shouldn't all functions make a
reasonable effort at promotion to try to do something reasonable? (E.g.,
the Numeric ufuncs generally do this correctly.)

If Python is a typed language, shouldn't we be able to determine 
what types are allowed as arguments and returned from functions 
without experimentation (and reverse engineering from the source code)?

Sorry for the length, but I'm just trying to figure this stuff out.
-- 
Charles Boncelet <boncelet at udel.edu>
University of Delaware
Newark DE 19716 USA
http://www.eecis.udel.edu/~boncelet/



More information about the Python-list mailing list