Sub topic 3
Dynamic typing and checking types with type()
This chapter assumes you can run Python code, use print(), and know what a statement is. It builds on the idea that an assignment binds a name to an object.
Types belong to objects, not names
In Python, every object has a type. A name has none. The name count is only a label, and it points to whatever object it was last bound to. This is dynamic typing: types are checked when the program runs, and a name can be rebound to an object of a different type.
The built-in type() function returns the type of the object a name currently refers to.
count = 3
price = 4.5
name = "Ada"
print(type(count), type(price), type(name))
count = "three"
print(type(count))The first print shows <class 'int'> <class 'float'> <class 'str'>. After count = "three", the second print shows <class 'str'>. Python raised no error, because the rebinding only moved the label to a different object. The old integer 3 is untouched.
Dynamic does not mean weak
Python is dynamically typed, but it is also strongly typed. It does not silently convert between unrelated types. Operations are checked against the objects' types when they run.
count = 3
try:
print(count + "apples")
except TypeError as e:
print("TypeError:", e)Adding an int and a str raises TypeError. Python does not guess whether you meant 3apples or a number. You must convert explicitly, a topic covered in later chapters.
| Language style | When types are checked | Example of the effect |
|---|---|---|
| Static | Before the program runs | Assigning text to an integer variable is rejected early |
| Dynamic | While the program runs | The same name can hold an int, then a str |
| Strong | On every operation | `3 + "a"` fails instead of converting |
Python is dynamic and strong.
Checking types
type() is useful for debugging, as in "why does this behave like text?" For decisions in code, isinstance() is usually better. It also accepts subclasses, and it can test several types at once.
value = True
print(type(value) == int)
print(isinstance(value, int))
print(isinstance(value, (int, float)))This prints False, True, and True. bool is a subclass of int. The exact comparison with type() rejects it, while isinstance() accepts it. Which one you want depends on the task, but isinstance() is the common choice.
Common errors
- Assuming a name keeps its type. A function that reads
totalmay receive an int in one call and a str in another. The error appears only when the bad operation runs. - Comparing types with
==in conditions. Preferisinstance()so subclasses work. - Reusing one name for unrelated data.
data = 5and laterdata = "5"is legal but invites mistakes. Use separate, descriptive names.
Exercise
After x = 10 and then x = x / 2, what does type(x) report, and why can the name change type?
Show answer
It reports <class 'float'>. Division with / always produces a float (5.0). Types belong to objects, so rebinding x to the new float object is allowed.
Exercise
Given flag = False, what do type(flag) == int and isinstance(flag, int) print?
Show answer
False and True. The exact type of flag is bool, not int, but bool is a subclass of int, so isinstance() accepts it.