Skip to content

Commit f996638

Browse files
author
Philipp Acsany
committed
Add python-mixins-course sample code
Publishes the code from the 'Understanding Mixin Classes in Python' video course so it can be offered as a free email opt-in download to the YouTube/free audience (course zip is otherwise members-only).
1 parent 3ec52ca commit f996638

7 files changed

Lines changed: 169 additions & 0 deletions

File tree

python-mixins-course/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Understanding Mixin Classes in Python
2+
3+
This folder contains sample code for the Real Python video course [Understanding Mixin Classes in Python](https://realpython.com/courses/understanding-mixin-classes-in-python/).
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Person:
2+
def __init__(self, name, age):
3+
self.name = name
4+
self.age = age
5+
6+
def as_dict(self):
7+
return {
8+
"name": self.name,
9+
"age": self.age,
10+
}
11+
12+
13+
class Car:
14+
def __init__(self, make, model):
15+
self.make = make
16+
self.model = model
17+
18+
def as_dict(self):
19+
return {
20+
"make": self.make,
21+
"model": self.model,
22+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class AsDictMixin:
2+
def as_dict(self):
3+
return vars(self)
4+
5+
6+
class Person(AsDictMixin):
7+
def __init__(self, name, age):
8+
self.name = name
9+
self.age = age
10+
11+
12+
john = Person("John", 42)
13+
print(john.as_dict())
14+
15+
16+
class Car(AsDictMixin):
17+
def __init__(self, make, model):
18+
self.make = make
19+
self.model = model
20+
21+
22+
toyota = Car("Toyota", "Prius")
23+
print(toyota.as_dict())

python-mixins-course/sequence.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from abc import ABC, abstractmethod
2+
3+
4+
class Sequence(ABC):
5+
@abstractmethod
6+
def __getitem__(self, index):
7+
pass
8+
9+
@abstractmethod
10+
def __len__(self):
11+
pass
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from abc import ABC, abstractmethod
2+
3+
4+
class Sequence(ABC):
5+
@abstractmethod
6+
def __getitem__(self, index):
7+
pass
8+
9+
@abstractmethod
10+
def __len__(self):
11+
pass
12+
13+
def __iter__(self):
14+
index = 0
15+
while index < len(self):
16+
yield self[index]
17+
index += 1
18+
19+
20+
class MyRange(Sequence):
21+
def __init__(self, stop):
22+
self.stop = stop # Assume that `stop` is >= 0.
23+
24+
def __getitem__(self, index):
25+
if 0 <= index < self.stop:
26+
return index
27+
raise IndexError
28+
29+
def __len__(self):
30+
return self.stop
31+
32+
33+
r = MyRange(10)
34+
for number in r:
35+
print(number)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from abc import ABC, abstractmethod
2+
3+
4+
class Sequence(ABC):
5+
@abstractmethod
6+
def __getitem__(self, index):
7+
pass
8+
9+
@abstractmethod
10+
def __len__(self):
11+
pass
12+
13+
def __iter__(self):
14+
index = 0
15+
while index < len(self):
16+
yield self[index]
17+
index += 1
18+
19+
20+
class MyRange(Sequence):
21+
def __init__(self, stop):
22+
self.stop = stop # Assume that `stop` is >= 0.
23+
24+
def __getitem__(self, index):
25+
if 0 <= index < self.stop:
26+
return index
27+
raise IndexError
28+
29+
def __len__(self):
30+
return self.stop
31+
32+
33+
r = MyRange(10)
34+
for number in r:
35+
print(number)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
from abc import ABC, abstractmethod
2+
3+
4+
class IterMixin:
5+
# __iter__ works IF the class inheriting the mixin
6+
# already provides __getitem__ and __len__.
7+
def __iter__(self):
8+
print("Hey from inside IterMixin.__iter__.")
9+
index = 0
10+
while index < len(self):
11+
yield self[index]
12+
index += 1
13+
14+
15+
class Sequence(IterMixin, ABC):
16+
@abstractmethod
17+
def __getitem__(self, index):
18+
pass
19+
20+
@abstractmethod
21+
def __len__(self):
22+
pass
23+
24+
25+
class MyRange(Sequence):
26+
def __init__(self, stop):
27+
self.stop = stop # Assume that `stop` is >= 0.
28+
29+
def __getitem__(self, index):
30+
if 0 <= index < self.stop:
31+
return index
32+
raise IndexError
33+
34+
def __len__(self):
35+
return self.stop
36+
37+
38+
r = MyRange(10)
39+
for number in r:
40+
print(number)

0 commit comments

Comments
 (0)