learnpython.org - an online interactive Python tutorial
harrismh777 at charter.net
Fri Apr 22 08:38:21 CEST 2011
Heiko Wundram wrote:
> The difference between strong typing and weak typing is best described by:
> Python 2.6.5 (r265:79063, Jun 12 2010, 17:07:01)
> [GCC 4.3.4 20090804 (release) 1] on cygwin
> Type "help", "copyright", "credits" or "license" for more information.
>>>> >>> 1+'2'
> Traceback (most recent call last):
> File "<stdin>", line 1, in<module>
> TypeError: unsupported operand type(s) for +: 'int' and 'str'
Yes. And you have managed to point out a serious flaw in the overall
logic and consistency of Python, IMHO.
Strings should auto-type-promote to numbers if appropriate.
This behavior should occur in input() as well. If a 'number' string is
entered and can be converted to a Python number (whatever I mean by that
at the moment) then the string should be converted to a number (int or
float as appropriate) and the input() should return a reference to the
number type ( a value ); otherwise, input() should return the string
entered, or throw a type error.
If an operation like (+) is used to add 1 + '1' then the string should
be converted to int and the addition should take place, returning a
reference to object int (2).
My feelings about this are strongly influenced by my experiences with
the REXX language on IBM's SAA systems--- OS/2 and VM/CMS. In REXX
everything is a string... everything. If a string just happens to be a
REXX number, then it can be manipulated as you might expect for a
number. Neither here, nor there... just that I believe Python could
take advantage of the "Python Number" concept and provide for auto-type
casting of string to number (int or float) as appropriate if the string
meets the Python Number requirements.
Just an idea... again, probably got beat up long before my time, I'm
More information about the Python-list