Why should i use python if i can use java

Martijn Faassen m.faassen at vet.uu.nl
Thu Jun 14 09:47:25 EDT 2001


Alex Martelli <aleaxit at yahoo.com> wrote:
> "Glyph Lefkowitz" <glyph at twistedmatrix.com> wrote in message
> news:mailman.991892413.1443.python-list at python.org...
>>
>> On 6 Jun 2001, Chris Goringe wrote:
>>
>> !snip!
>>
>> > python doesn't [have ...]
>> > strong typing (love it or hate it...)
>>
>> Hate it :)

> What _I_ hate is the confusion between weak vs strong typing
> AND static vs dynamic typechecks, though it's a classic one:-).
> Weak typing, properly, is what (e.g.) Perl has, where you can
> use any scalar wherever, and it will try to guess what you
> mean, freely treating strings as numbers, viceversa, etc.
> What Python has (like, say, Scheme) I'd call strong typing
> that is dynamically enforced.

I always see this but I'm a bit skeptical about it. Python doesn't really
do any type checking for class instances; it checks whether there is a 
method that is of the right name and has the right amount of arguments 
somewhere on the instance or its superclasses, and calls it if so. There is
no 'is this the expected class?' check anywhere, not even at runtime.
You can make the same case for built-in types. 

*Some* methods of types and classes do a class/type check, of course. Strings
and integers decide to give up if they're __add__-ed to each other, but
that could be considered a feature of the extension module (okay, they're
important ones), not of the language. It certainly doesn't have to be
that way. Python itself doesn't care one way or the other. I think saying
"Python is strongly typed" can be misleading and confusing. "Strongly typed"
and "Dynamically typed" combined doesn't seem to make much sense, if
that language allows users to create their own types/classes.

>  E.g.:

> D:\py21>python
> Python 2.1 (#15, Apr 16 2001, 18:25:49) [MSC 32 bit (Intel)] on win32
> Type "copyright", "credits" or "license" for more information.
> Alternative ReadLine 1.4 -- Copyright 2001, Chris Gonnerman
>>>> "ciao"[1.0]
> Traceback (most recent call last):
>  File "<stdin>", line 1, in ?
> TypeError: sequence index must be integer
>>>>

> That's *WEAK* typing?!  I call it pretty strong indeed --
> and love it!  (Java works similarly here -- it's an
> error to use a non-integer as array index -- although
> C and C++ happily accept the float and truncate it...).
> It's *DYNAMICALLY* checked and enforced, not statically.

But only in a few places, mostly for built-in types. Those happen to be the
*important* places but any other checking you'll have to come up
with yourself. I don't think 'call this method can see whether you 
succeed' strongly typed.
 
> I think it's more precise and useful to draw the
> distinction as static vs dynamic than as strong
> vs weak, no matter which way one wants to argue
> for or against it.

That is true; strong versus weak is more a feature of the actual
classes (types) you're using in a dynamically typed language than a feature
of the language itself.

Anyway, I've done my bit to counter the 'Python is strongly typed' meme.
I suspect that meme is around because there are lingering ideas that 
strong type checking is a Good Thing. I think some run-time type or
interface checking can be a good thing. But I'd hardly call what Python
is doing 'strong'.
 
Python has a TypeError exception that methods can raise, and
many methods of built-in objects do this because guessing would be
'magic'.

Regards,

Martijn
-- 
History of the 20th Century: WW1, WW2, WW3?
No, WWW -- Could we be going in the right direction?



More information about the Python-list mailing list