DEV Community 👩‍💻👨‍💻

Cover image for Python is strongly, dynamically typed. What does that mean?
icncsx
icncsx

Posted on

Python is strongly, dynamically typed. What does that mean?

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
Enter fullscreen mode Exit fullscreen mode
1 + "1" // "11" in JavaScript
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
kallmanation profile image
Nathan Kallman

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!

Collapse
icncsx profile image
icncsx Author

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.

🌚 Browsing with dark mode makes you a better developer.

It's a scientific fact.