Chapter 13 – Class and Type¶
Overview¶
This chapter explores Python’s approach to type and how it relates to classes and instances. It clarifies what “type” means in Python and how strong vs. dynamic typing is realized. :contentReference[oaicite:3]{index=3}
Three Types of Type¶
There are multiple notions of type in programming:
The runtime type of an object (type(obj))
The static annotated type (type hints)
Conceptual type based on behavior
Primitive Data Types¶
Built‑in types like int, str, and float have fixed behaviors but are still Python objects.
Class‑Based Type¶
Every class defines a type. When you create a class, you create a new type.
Hierarchical Typing¶
Python’s inheritance hierarchy means types can participate in subclass relationships. For example:
isinstance(obj, Base)
Instance and Subclass¶
isinstance and issubclass help test whether an object belongs to a type or whether a class is derived from another.
Duck Typing¶
Python generally uses duck typing — if an object has the right methods and attributes, its type often doesn’t matter for correctness:
def quack(duck):
duck.quack()
As long as duck has a quack method, this works irrespective of actual type.
Summary¶
Python’s notion of type combines runtime objects, class definitions, and behavioral expectations rather than rigid compile‑time constraints.