This directory contains exercises focused on how to organize your Python code using classes, objects, and proper structure. These topics teach you the fundamental concepts of object-oriented programming in Python.
-
Classes and Objects: The foundation of organizing related data and behavior together. Classes are blueprints for creating objects that model real-world entities or abstract concepts in your programs.
-
Instance Methods: Functions that belong to objects and can access/modify the object's data. The
selfparameter is Python's way of referring to the current object instance. -
Parameters and Return Values: How to design functions and methods that accept input, process it, and return meaningful results. This is essential for creating reusable, testable code.
-
Nested Objects: Real applications often have objects that contain other objects. Understanding composition and relationships between objects is crucial for modeling complex systems.
These concepts build upon each other:
- You create classes to define what objects can do
- You use instance methods to implement the behavior
- You design parameters and return values for clean interfaces
- You compose nested objects to model complex relationships
- The self parameter: How Python passes the current object to methods
- init method: Python's constructor for initializing objects
- Instance vs class attributes: Data that belongs to objects vs the class
- Property decorators: Pythonic getters and setters using @property
- String representation: str and repr for readable object output
- Duck typing: "If it walks like a duck and quacks like a duck, it's a duck"
Each topic directory contains:
- README.md: Detailed exercises with clear instructions
- Python file: Hands-on practice file with class definitions to complete (e.g., classes.py, methods.py)
- NOTES.md: Comprehensive explanations and examples
Work through these topics in order to build a solid foundation in Python object-oriented programming.
Navigate to any topic directory and:
# Run the exercises directly
python <filename>.py # e.g., python classes.py, python methods.py
# Or work interactively
python3 -i <filename>.py # e.g., python3 -i classes.py
# This loads the file and drops you into the Python REPL
# where you can create objects and test methods