Instructions
This lesson covers Inheritance, Operator Overloading, and Iterators. Modify the code in the editor boxes below and click "Run Code" to see the result.
Note: Python acts strictly. Watch your indentation!
Exercise 1: Basic Inheritance
Inheritance allows us to define a class that inherits all the methods and properties from another
class.
Task: Create a class implementation where Student inherits from
Person.
The Person class should have a introduce() method. Override it in
Student to include the major.
Exercise 2: Operator Overloading (+)
We can define how operators like + behave for our custom objects by implementing magic
methods like __add__.
Task: Implement __add__ for the Vector class so that
v1 + v2 returns a new Vector with summed coordinates.
Exercise 3: Iterator Class (StudentList)
To make an object iterable (usable in a for loop), we need to implement
__iter__ and __next__.
Task: Complete the StudentList class. It should hold a list of
students and allow iteration one by one. Raise StopIteration when done.