Hacker News new | ask | show | jobs
by schmuelio 1401 days ago
Your example doesn't show static typing (declaration/re-declaration is the same syntax as assignment in Python), but I think OP was thinking of Strong/Weak?

Python is Dynamically typed because:

    >>> a = "foo"
    >>> a / 3
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unsupported operand type(s) for /: 'str' and 'int'
This isn't a compile error.

Edit: Also for the re-declaration thing, you can see this using id(). Python seems do do some fancy value stuff behind the scenes but you can see the variable changing its identifier in the following example:

    >>> a = "foo"
    >>> id(a)
    140105790230896
    >>> a = "foo"
    >>> id(a)
    140105790230896
    >>> a = "bar"
    >>> id(a)
    140105790230512
    >>> a = 1
    >>> id(a)
    140105793782000
    >>> a = "foo"
    >>> id(a)
    140105790230512
As for strong/weak, I think it's a bit more fluid because I can't seem to find a set definition that everyone agrees on. Some people consider weak typing to be when the language implicitly casts or converts types for you, which Python does not do:

    >>> a = "1"
    >>> a / 3
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
    TypeError: unsupported operand type(s) for /: 'str' and 'int'
    >>> type(a)
    <class 'str'>
Except sometimes it kind of does? The divide operator converts int into float "implicitly" even when both inputs are int. So type conversion is happening behind the scenes (I don't know if you would class this as "implicit" type conversion, maybe it depends on where it happens?):

    >>> a = 10
    >>> type(a)
    <class 'int'>
    >>> b = 1
    >>> type(b)
    <class 'int'>
    >>> type(a/b)
    <class 'float'>
2 comments

> As for strong/weak, I think it's a bit more fluid because I can't seem to find a set definition that everyone agrees on.

Yes, I've been looking into this lately, and the terms are messy. People tend to use strong to mean "strict in ways I like" and weak to mean "permissive in ways I don't like."

Division is a good example of this ambiguity. It always results in a float, so if you divide two integers, even ones that are evenly divisible, you get a float, so that's kind of a conversion, right? But on the other hand, I don't think a function that was defined as taking two integers and returning a float would be considered an implicit conversion, even if it were overloaded to also accept various combinations of float and integer.

Gary Bernhardt has a good discussion: https://www.destroyallsoftware.com/compendium/types?share_ke...

Thanks for the correction and the comprehensive examples!