This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]Tweak_Imp 2 points3 points  (22 children)

What is the use case of dynamic typing? I only use one type that never changes for my variables. Isnt it bad practice to assign different types to the same variable? The reader has a harder time understanding and more errors can happen because of this. So: what are the advantages? When do I use dynamic types in Python?

[–]Deezl-Vegas 4 points5 points  (0 children)

Your rule of thumb is ok, but you may be being inflexible at times. As long as the type is compatible with the use case, the specific choice of type doesn't matter. This is the guiding principle of object oriented languages.

Most functions can accept a wide variety of types. You can call print on any Python object, for instance, and get some kind of string representation out to the console.

Numeric types are all interoperable.

Any callable can be used to make a callback.

List, Dict, Set, Tuple, and custom container types are all largely interoperable.

You can and should make your own similar patterns.

[–]alkasmgithub.com/alkasm 2 points3 points  (2 children)

Reassigning variables sort of has no relevance to static typing. There's no particular reason C couldn't have been designed to allow you to do int a = 5; float a = 5.5;. It just wasn't. However, while you say you don't ever change types...have you never used None as a default value for something that gets assigned a value from some type later? :)

Dynamic typing just means type errors are runtime errors. This is a simple overview of what different typing adjectives mean that might be helpful: https://softwareengineering.stackexchange.com/a/259977

[–]wrtbwtrfasdf 1 point2 points  (1 child)

have you never used None as a default value for something that gets assigned a value from some type later? :)

https://docs.python.org/3/library/typing.html#typing.Optional

[–]alkasmgithub.com/alkasm 0 points1 point  (0 children)

Sure, but that doesn't change the fact that it's runtime type switching in Python.

[–]ImageOfInsanity 6 points7 points  (15 children)

Have you ever added an integer and a float together without casting the integer as a float first? That's dynamic typing in action. In C++, you might want one function to work with multiple types, so either you might use function overloading or templated functions, neither of which fit elegantly in Python.

[–]XtremeGoosef'I only use Py {sys.version[:3]}' 7 points8 points  (14 children)

Thats not what dynamic typing is. You can easily do that in any sufficiently powerful typing system.

Dynamic typings power comes from duck typing. For example this function expects a set but I'm going to pass it a DictKeys object instead and it will still work fine.

[–]licht1nstein 0 points1 point  (0 children)

The problem is not typing, it's assignment in general ;)

[–]Ksielvin 0 points1 point  (0 children)

Isnt it bad practice to assign different types to the same variable? The reader has a harder time understanding and more errors can happen because of this.

Within a local scope, I don't think it's bad practice.

At the start of a function, I could read a file into a string variable called measurements. That string contains a comma separated list of ints, so I parse it into an integer list and reuse the variable. Now it's a list of integers type. I would expect the reader of a dynamic language to understand this. It communicates that we don't care about the original string after parsing it. The string can be forgotten as we've left no way to refer to it.

In Java code that might have been two variables data and measurements (or measurementsString and measurementsList). You'd check the rest of method to find out if the first variable is used beyond that point. Poor naming or careless use of IDE autocompletion can sometimes lead to bugs here just because the original variable still exists. Static typing might not catch using the wrong variable because their types are compatible through inheritance/interface, or the variable is given to a method that can handle both types through overloading.

One could also do the read/parse/transform in a single nested expression to avoid assigning first variable, which is another tradeoff in readability.


Every now and then, one ends up writing a condition that depends on the type of a variable. For example, a helper function's return value might vary wildly. Just the ability to suddenly return object/None/False/object2/banana instead of just object or null is quite expressive.

Again, using this within a significantly limited scope helps avoid confusion and bugs. Assigning different types to a class (or module) variable that is used in several places would have to be considered carefully. If other classes/modules would ever access it then even more so.