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:
>>> 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:
>>> 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";
:
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:
$ 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:
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:
$ 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: