Python complaints

Tim Peters tim_one at email.msn.com
Fri Dec 17 03:27:15 EST 1999


[Charles Boncelet]
> ...
> If Python is a typeless language, then shouldn't all functions
> make a reasonable effort at promotion to try to do something
> reasonable?

It's a strongly typed language -- more strongly typed than C, for example.
It's not *statically* typed, though.  It generally tries hard *not* to do
promotions that aren't "obvious".  It was years before, e.g., "int()" was
liberalized to accept string arguments.  It's not trying to do the merely
reasonable, it's trying to do the hard-to-be-surprised-by.

A fellow at work was trying to debug a piece of Perl that had this
subexpression:

   len(@a)

Perl tries very hard to do something reasonable:  first it sees an array in
scalar context, so says "OK, I'll return the length of the array!".  Then
len() sees the length, which is an integer, and says "Hey, I don't know how
to do that!  So I'll coerce the integer to something I *do* know how to take
the length of -- a string!".  So his array with 1,000-some elements returned
4 (1234 -> "1234" -> a string of length 4).

There are no similar msgs about Python promotion surprises over on
comp.lang.perl.misc <wink>.

> (E.g., the Numeric ufuncs generally do this correctly.)

NumPy's users are presumed to be mathematical grownups for whom "the usual"
mathematical coercions are indeed "usual".  math.sqrt(-30.2) in core Python
is almost certainly due to someone e.g. using a numerically naive method for
computing sample variance <1/sqrt(2*pi) wink>.  That is, as even in the
IEEE-754 standard, sqrt(-x) is "an error" to most people.

> 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)?

Yes, but that's a long and difficult battle in a language without names for
most of its conceptual types.  The Types-SIG is trying to address this in
the months it isn't comatosely depressed.

It will take a while to get used to what you can and can't get away with!
As general hints, don't try to be clever all the time, and get very
comfortable with interactive mode.  Most things are actually quite
reasonable.

the-night-stars-look-random-at-first-too-ly y'rs  - tim






More information about the Python-list mailing list