News for hackers. Stuff that matters.
Re:Hidden features of Python by masklinn on Wednesday July 14, 2010 @02:31AM (#1513835)
> I was making a glib joke, but to answer your question this is the tradeoff of using a not-so-strongly-typed language.

Not in this case. In this case it results from:

1. Python historically not having booleans and using 0 and 1 instead

2. As a result, `bool` inherits from `int` for compatibility reasons

    >>> issubclass(bool, int)
    True
with False mapping to 0 and True to 1 for compatibility purposes

    >>> int(False), int(True)
    (0, 1)
3. Add isn't overridden on bool

    >>> bool.__add__
    <slot wrapper '__add__' of 'int' objects>
therefore, when adding two booleans you're really adding two integers.

Most non-strongly-typed languages will not have the following behavior

    >>> "foo" + 3
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: cannot concatenate 'str' and 'int' objects
Re:Hidden features of Python by masklinn on Wednesday July 14, 2010 @07:27AM (#1514253)
In fact, to expand on the latter part of my comment, "weak typing" is generally understood as implying implicit type coercions or conversions at runtime. PHP and Javascript famously convert between numbers and strings depending on a bunch of factors (and usually the wrong way leading to unreliable results).

In that sense, Python is a very strongly typed language: from the top of my head, the only example of implicit type conversion in Python is the promotion of `int` to `long`, which has been removed from 3.x (all integer types were unified). All other conversions have to be explicit as far as I remember. That makes Python more strongly (but less statically) typed than Java (see the very strange overloading of the `+` operator when any operand is a string, as well as autoboxing since 1.5) and C# (C# allows for implicit type conversions, including in userland, and the cast operator doubles up as an explicit type conversion operator which I find terrible)