Eiffel better than Python ?
D-Man
dsh8290 at rit.edu
Tue Jul 3 18:19:33 EDT 2001
On Tue, Jul 03, 2001 at 09:32:06PM +0000, Dublanc, David wrote:
| What is exactly weak typing ?
Weak typing is what the shell or perl do : anything can be used
anywhere and it only has the type "variable" (or whatever you want to
call it). Something like the following :
#!/usr/bin/perl
print 2 + "1" ;
print 2 + "1a" ;
print "1" + "2" ;
print "1a" + "2" ;
print "1" + 2 ;
print "1a" + 2 ;
What you get is an interesting auto-coercion between strings and
integers because there is no difference, as far as perl is concerned.
Some of those, in Python, would be type errors :
>>> print 2 + "1"
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: unsupported operand types for +
>>>
The closest thing that might work in Python would be
>>> print 2 + int( "1" )
3
>>>
but it doesn't always work (as it does in Perl) :
>>> print 2 + int( "1a" )
Traceback (most recent call last):
File "<stdin>", line 1, in ?
ValueError: invalid literal for int(): 1a
>>>
Another indication that perl has weak typing is the following behavior :
$ perl
print $undefined_variable_name + 10 ;
10
$
In perl if a variable doesn't exist, and it is used in numerical
context it is treated as 0, or as the empty string if used in string
context. In Python that would raise a NameError exception.
| I believed that Python was weak typing !
As you can see above, Python actually checks the types to ensure that
they are sane for the given operation. It checks them _dynamically_
(at run time) as opposed to statically (at compile time).
There are 4 forms of type checking :
weak
strong
dynamic
static
Some people mistakenly consider dynamic == weak and static == strong,
but they are 2 different axis.
HTH,
-D
More information about the Python-list
mailing list