Static Typing in Python

Ludovic Aubry no_ludal_spam at logilab.com
Mon Mar 15 12:28:39 EST 2004


On Mon, 15 Mar 2004 14:41:10 +0100, Jacek Generowicz wrote:

> Python is strongly typed because:
> 
> - lots of type checking is done
> - few implicit type conversions are done.
> - No operations which are inappropriate to the object(s) in question
>   are allowed.
While I agree with the above...

I think your arguments in the following are mostly wrong:
> Try this ...
> 
>     #include <iostream>
>     
>     void foo() {}
>     
>     int main() {
>     
>     float fs[128];
>     
>     char c = 'a';
>     // add integer to character
>     c = c + 1;
In C char is an integer type, it just happens that char is used to
represent characters. I'd rather say there is no character type in C (just
like in Python by the way)
This is exactly the same as if you did in
python:
   >>> 1L+2
   3L
The difference is that in C the cast is implicit and done before calling
the __add__ operator (when it is redefined)

>     // Use a character to subscript an array std::cout << fs[c]
<<
>     std::endl;
>     // Out of bounds array access
>     std::cout << fs[200] << std::endl;
>   
>     int* ip;
>     ip = (int*)(void*)(&foo);
>     // Add an integer to a function
>     std::cout << 3+(*ip) << std::endl;
You did nothing of what you said; you just added 3 to a pointer to an int
You even did an explicit cast from your function pointer into a pointer to
int.
Doing what you said with g++ ie:
{
  foo+=3;
}
gives the error:
"error: pointer to a function used in arithmetic"

>     return 0;
>     }
>     }
> So, here we have four examples of weak typing in C++:
After closer examination this leaves the out of bound if you can count
that as weak typing.
But I would definitely add the automatic casting as a weak typing example...

> Try as you will, Python would not allow you to do any of these things,
> because it is _strongly typed_ with respect to these operations on these
> types.
Python will allow you to multiply string with integers for example ;)

-- 
Ludovic Aubry                                 LOGILAB, Paris (France).
http://www.logilab.com   http://www.logilab.fr  http://www.logilab.org




More information about the Python-list mailing list