Python is strongly typed
Let's start with the strong typing aspect. Strong typing
means that the type of an object doesn't change in unexpected ways. A string containing only digits doesn't magically become a number, as may happen in weakly typed languages like JavaScript and Perl. Every change of type requires an explicit type conversion (aka casting).
1 + "1" # TypeError in Python
1 + "1" // "11" in JavaScript
Python is dynamically typed
Let's talk about the opposite of dynamic typing (static typing) for contrast. In a statically typed language such as C++, you need to fix the type of the variable. This type will be the same as that of the object which is assigned to that variable.
int x; // declare step
x = 4; // assign step
In a dynamically typed language, the interpreter does not assign a type to the variable per se because the type can change at runtime. If you ask a variable its type, it will give you the type of the object it is currently assigned to at that moment.
x = 4
print(type(4)) # at this moment, x points to an integer
x = "Hello, world"
print(type(x)) # and at this moment, x points to a string
Top comments (2)
Amazing! I never realized python was an actual example of the difference between strong v loose and static v dynamic typing. I always thought splitting hairs on that was a little hypothetical and in practice always paired up like peanut butter and jelly.
Thanks for the article!
What's also interesting to note is that those two have no causal relationship. A strongly typed language doesn't imply that it's a statically typed language and vice versa. Glad you liked the post! More to come.