Loading video player…

Dynamic vs Static

In this lesson, you’ll learn about type systems, comparing dynamic typing and static typing. All programming languages include some kind of type system that formalizes which categories of objects it can work with and how those categories are treated.

Dynamic Typing

Python is a dynamically typed language. This means that the Python interpreter does type checking only as code runs, and the type of a variable is allowed to change over its lifetime. Here are a couple of examples that demonstrate those ideas:

Python
>>> if False:
...     1 + "two"  # This line never runs, so no TypeError is raised
... else:
...     1 + 2
...
3
>>> 1 + "two"  # Now this is type checked
TypeError: unsupported operand type(s) for +: 'int' and 'str'

In the first example, the branch 1 + "two" never runs, so it’s never type checked. The second example shows that when 1 + "two" is evaluated, it raises a TypeError since you can’t add an integer and a string in Python.

In this next example, you see if variables can change type:

Python
>>> thing = "Hello"
>>> type(thing)
<class 'str'>

>>> thing = 28.1
>>> type(thing)
<class 'float'>

type() returns the type of an object.

Static Typing

The opposite of dynamic typing is static typing. Static type checks are performed without running the program. In most statically typed languages, for instance C and Java, this is done as your program is compiled. The type of a variable is not allowed to change over its lifetime.

In this Hello World example in Java, look at the middle section, where String thing; is statically defined as a type of String and then assigned the value thing = "Hello World";:

Java
public class HelloTypes {

    public static void main(String[] args) {

        String thing;
        thing = "Hello World";

        System.out.println(thing);
    }
}

This is not a course on Java, so don’t worry about the specifics of how to create Java code. The purpose of this example is to show you that there are extra steps in most statically typed languages.

In this next example, you would use the command javac to compile the program. This creates a new file with the same name, but a different extension .class instead of .java. That is the file that can be run using the java filename.class command:

Shell
$ javac HelloTypes.java
$ java HelloTypes.class
Hello World

If you were to try to reassign thing to a value that is of a different type, you will not get an error initially. Only when the code is compiled would you see the error:

Java
public class HelloTypes {

    public static void main(String[] args) {

        String thing;
        thing = "Hello World";

        thing = 42;

        System.out.println(thing);
    }
}

The line thing = 42; is attempting to change the type of thing from a string to an int. If you compile this code, you will see the error:

Shell
$ javac Hellotypes.java
HelloTypes.java:8: error: incompatible types: int cannot be converted to String
    thing = 42;
1 error

Python will always remain a dynamically typed language. However, PEP 484 introduced type hints, which make it possible to also do static type checking of Python code.

Here are a few resources on bpython, the REPL(Read–Eval–Print Loop) tool used in these videos:

00:00 In this video, I’m going to compare dynamic typing and static typing. When talking about type systems, you’ll hear comparisons of dynamic versus static often. Python is a dynamically typed language.

00:12 The Python interpreter does type checking only when the code runs. As you execute a line of code, as you’ll see in an example next, that’s when the type checking occurs.

00:23 And also, the type of a variable is allowed to change over its lifetime. Let me show you what both of those look like. For the examples where I’m running Python in a REPL, I’ll be using a REPL replacement known as bpython.

00:37 So instead of typing python or python3 and getting the standard REPL where you can then type in commands, I’ll be typing bpython.

00:47 I have a link included at the bottom of this video to show you about how to install it if you’d like to use it.

00:56 For dynamic typing, if you created a statement such as this, if False: 1, the integer 1, plus (+) the text string "two", just note that this line’s never going to run, so no TypeError is going to be raised.

01:14 But this else statement will, and if I put two integers being added together, 1 + 2,

01:21 when you hit Enter enough times there you’ll get down and you’ll see that it correctly added them together and gave you the integer of 3. What if you just tried to run this line by itself, 1 + "two"?

01:33 This is going to be type checked.

01:38 And you can see here, it created that TypeError that, if you’re going to use the + operator there, that an int and a str (string) are unsupported operand types.

01:47 So those are incompatible. You can’t use the + operator between the two. And the Python interpreter only threw that exception of a TypeError when the code was run.

01:59 Let’s check to see if variables can change types. We’ll start with an object named thing that equals "Hello World". Okay, so what is thing?

02:09 You can use the built-in function type() and type in the object thing. And that will say that that is the type str. Well, later on, if you suddenly said in your Python program thing = 28.1, now what is thing? If you use the type() function again, it’ll say that’s now a float.

02:29 So, when you run that assignment of thing = 28.1, the Python interpreter changes the type for thing into a float, dynamically.

02:42 In the case of static typing, things are very different. Static type checks are going to be performed without running the program, actually. In most statically typed languages, that type of checking is done when the program is compiled, before it can actually run. So there’s an extra step of compilation, which I’ll show you.

03:04 The type of variable is not allowed to change over its lifetime. It’ll be fixed as it’s defined. Now, there are exceptions to that, which I don’t want to get too deep into, where you can cast a variable into different types depending on the type of language, but generally, the type of variable is not allowed to change over its lifetime.

03:25 Let’s look at that. Let me show you a quick example from a statically typed language. Consider this simple “Hello, World!” program in Java. Here, you’re creating a variable named thing, and here you’re statically assigning it a type of String, and then here you’re assigning the value of an actual string of text. To be able to run this,

03:52 I would say javac, which is the command to compile this file HelloTypes.java.

04:01 At that point, once it’s compiled, if there are no errors, it should come back and just shows us here. Up in my browser, you’ll see that it actually changed HelloTypes.java and compiled it into HelloTypes.class.

04:14 Now this isn’t a tutorial on Java, I just want to show you that that file didn’t exist before and was created by running this command. To run the program, I can actually say java HelloTypes,

04:31 and there’s my simple program that ran. Okay. What if you were to, in the code, change it to say, “Well, actually now thing = 42,” changing it into an int, assigning it just like you would in Python. Well, I’d have to compile it again.

04:49 So, I saved. Now that I’ve saved, to compile—javac HelloTypes.java. At that point of compiling, it says incompatible types: int cannot be converted to String, and it gives that 1 error. So it won’t even compile the program into the .class version of it.

05:09 Python will always remain a dynamically typed language. I’ll include some links below this text that show the reasoning behind this. And with recent changes, if you desire to have some of the features of a statically typed language, you can use them inside of Python now with features like type hints and the ability to do type checking.

05:29 PEP 484 introduced type hints—you can see the link below this video—and then there’s additional tools to perform static type checks using those hints. You’ll get a chance to try them out further on in this course. In the next video, you’ll explore duck typing.

Become a Member to join the conversation.