>>> 1 + "0"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
This is what strong typing refers to. >>> 1 + "0"
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'int' and 'str'
This is what strong typing refers to. In [1]: 1 + 2.5
Out[1]: 3.5
In [2]: "foo" * 3
Out[2]: 'foofoofoo'
In [3]: [1, 2] * 3
Out[3]: [1, 2, 1, 2, 1, 2] In [1]: "foo" + 3
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-1-21582e79f06e> in <module>()
----> 1 "foo" + 3
TypeError: cannot concatenate 'str' and 'int' objects
That's because the "special" method "__add__" implemented by the string class will raise TypeError if it gets any object that isn't an instance of a subclass of string.In a way it is kind of funny to call it, "strongly typed," but it does work. Maybe it should be called, "instancely-typed."
It seems to make 'strongly typed' pretty meaningless, and this definition is probably _not_ what anyone who says they want a "strongly typed" langauge is using, so it hardly counters them to say that python is "strongly typed" under another definition, it's just confusing them with semantics.
(Of course, the people who say they want 'strongly typed' may have no idea what they're actually talking about, but wouldn't it be better to educate them then to take advantage of their ignorance to push your pro-python agenda?)
In general, when someone is talking about strong typing, they are talking about silent failure for unintuitive or ambiguous constructs. For example, if the expression
x = '1' + 1
results in an error, you are probably using a strongly-typed language. In C, this is equivalent to x = 32;
In javascript you get x = '11'
In PHP you get x = 2
These are examples of weak typing.You could also describe Python's type system to be dynamic in that instances of the type meta-class define the constraints and objects (instances of a class) can have constraints added and removed at run-time. Python is still fairly strong in this regard in that the built-in classes are immutable (ie: it is a TypeError to assign a bound method to an attribute on a built-in class such as str).
I suggest "instancely-typed" because categories, unions, and type theory. I'm only coming to grips with that in that OCaml's type solver can be both awesome, annoying, and cryptic. And at the end of the day I'm still not sure what it's buying me other than proving exhaustive pattern matches in certain conditions, fast pattern dispatching, and requiring specialized operators (+, -, , / for ints... +.,-.,.,/. for floats... etc). I'm sure the enlightenment will come when it stops becoming such a PITA to write a basic program.
Sometimes not having to satisfy the constraints up-front makes exploratory programming (where the constraints are not specified and known up front) easier. Python is going the annotation route in newer versions of the language which is rather useful so that tools could be written to verify consistency up-front (or at least provide hints).
Otherwise, you'd run into Python's strong type system when doing an incompatible operation.
I particularly like Python's arbitrary-precision integers, where ints are implicitly converted to longs, allowing you to have the performance of ints without the problem of integer overflows.
I think the reality is "strongly typed" doesn't really mean much.
The interpreter/type system is doing nothing implicitly behind the scenes to coerce the objects.
As most OO languages, Python has function overloading based on the type of the parameters. The fact that those operators do different things when operating on different types is evidence that Python has strict types, not loose.
There is no dynamic dispatch based on types in the interpreter, you have to manually do it in the body of the method, of which you have one.
Python 2.7.1 (r271:86882M, Nov 30 2010, 10:35:34)
[GCC 4.2.1 (Apple Inc. build 5664)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> 1/2.3
0.4347826086956522
>>> 1 / 2.3
fish: Job 2, 'python' terminated by signal SIGSEGV (Address boundary error)
(I didn't expect that last part.) Python 2.7.5 (default, Nov 3 2014, 14:26:24)
[GCC 4.8.3 20140911 (Red Hat 4.8.3-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 1/2.3
0.4347826086956522
>>> 1 / 2.3
0.4347826086956522
Identical output under Python 3.3.2Yep -- it seems like a readline problem. As you can tell, I don't use this machine for Python.
There are many popular and weakly typed languages in use today.
"[object Object]"
> {} + []
0
JS and PHP are the prototypical examples of weak typing.