Static Typing in Python

Premshree Pillai premshree_python at yahoo.co.in
Mon Mar 15 21:12:34 EST 2004


 --- Peter Hickman <peter at semantico.com> wrote: >
Premshree Pillai wrote:
> > Like in C, C++, etc, Python too is
> > strongly typed, i.e., variables are necessarily
> bound
> > to a particular type.
> 
> Python 1.5.2 (#1, Jan 31 2003, 11:01:49)  [GCC 2.96
> 20000731 (Red Hat 
> Linux 7.2 2 on linux-i386
> Copyright 1991-1995 Stichting Mathematisch Centrum,
> Amsterdam
>  >>> a = 1
>  >>> a
> 1
>  >>> a = "fred"
>  >>> a
> 'fred'
>  >>> a = open
>  >>> a
> <built-in function open>
>  >>>
> 
> So here was have a variable 'a'. In the first
> instance I set it to a 
> number. Then I set it to a string. Now in a strongly
> typed language, 
> such as C or C++, would be screaming that "fred" was
> not an integer. 
> Then we set it to, in effect, a pointer to a
> function. Again no error.
> 
> Could you please clarify how this consitutes Python
> being 'strongly typed'?
> -- 
> http://mail.python.org/mailman/listinfo/python-list

Well, that was not what I meant. When I said that
variables are bound to a particular type, I meant that
variables necessarily have a specific type; I didn't
say that you cannot change its type. Consider this:

>>> str = 10
>>> len(str)
Traceback (most recent call last):
  File "<pyshell#1>", line 1, in ?
    len(str)
TypeError: len() of unsized object
>>> 

This was what I meant. See the following:

>>> foo = "x"
>>> foo = foo + 2
Traceback (most recent call last):
  File "<pyshell#3>", line 1, in ?
    foo = foo + 2
TypeError: cannot concatenate 'str' and 'int' objects
>>> 

The above returns a TypeError in Python, but not in
PHP, which is weakly typed.

Get it?

-Premshree Pillai 

=====
-Premshree
[http://www.qiksearch.com/]

________________________________________________________________________
Yahoo! India Insurance Special: Be informed on the best policies, services, tools and more. 
Go to: http://in.insurance.yahoo.com/licspecial/index.html




More information about the Python-list mailing list