Lecture 13: Inheritance
Autumn 2026
1
Sli.do
#cse163
Questions?
Icebreaker
Favorite candy?
CSE 163
CSE 163: Inheritance
Announcements
2
Questions?
CSE 163: Inheritance
Review: Components of a Class
3
class SpiderFolk:
def __init__(self, name: str) -> None:
self.name: str = name
def thwip(self) -> None:
print(self.name + ‘: Thwip!')
A class definition
An initializer that sets fields (state)
A method (behavior)
CSE 163: Inheritance
Private in Python
4
class SpiderFolk:
def __init__(self, name: str) -> None:
self._name: str = name
def thwip(self) -> None:
print(self._name + ': Thwip!')
CSE 163: Inheritance
Value vs. Referential Equality
Consider the following code
Compare these lists in two different ways
5
l1 = [1, 2, 3]
l2 = [1, 2, 3]
l3 = l1
CSE 163: Inheritance
Defining Value Equality with Objects
6
CSE 163: Inheritance
Special (Dunder) Methods
7
Syntax | Method |
x < y | x.__lt__(y) |
x > y | x.__gt__(y) |
x == y | x.__eq__(y) |
x <= y | x.__le__(y) |
x >= y | x.__ge__(y) |
print(x) | print(x.__str__()) |
x[i] | x.__getitem__(i) |
x[i] = v | x.__setitem__(i, v) |
CSE 163: Inheritance
Lambdas
8
def get_dog_name(d):
return d.get_name()
sorted(dogs, key=get_dog_name)
sorted(dogs, key=lambda d: d.get_name())
CSE 163: Inheritance
Inheritance
9
CSE 163: Inheritance
Inheritance Example
10
class Dog:
def __init__(self, name: str) -> None:
self._name: str = name
�
def bark(self) -> None:
print(self._name + ': Woof')
class ServiceDog(Dog):
def __init__(self, name: str, service: str) -> None:
super().__init__(name)
self._service: str = service
�
def bark(self) -> None:
print(self._name + ‘: *alert bark*')
CSE 163: Inheritance