So far, you’ve written programs as a series of steps: read some data, do some calculations, print results. This is called a procedural style.
Object-oriented programming (OOP) is a different way of organizing your code. Instead of just thinking in terms of steps, you think in terms of things (objects) and what they can do (methods) and what they know (attributes).
OOP is especially useful when:
- Your program models real-world things (like users, bank accounts, products, games).
- Your code is getting bigger, and you want it to be easier to understand and maintain.
- You want to group related data and behavior together.
At the core of OOP in Python are:
- Classes – blueprints for objects
- Objects – actual things created from those blueprints
- Attributes – data stored on an object
- Methods – functions that belong to an object
- Special methods – like
__init__, used for setup - Encapsulation – hiding internal details, exposing a simple interface
- Inheritance – creating new classes based on existing ones
The rest of this chapter introduces each of these ideas with simple, concrete examples.