Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions python-mixins-course/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Understanding Mixin Classes in Python

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/).
22 changes: 22 additions & 0 deletions python-mixins-course/as_dict_mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Person:
def __init__(self, name, age):
self.name = name
self.age = age

def as_dict(self):
return {
"name": self.name,
"age": self.age,
}


class Car:
def __init__(self, make, model):
self.make = make
self.model = model

def as_dict(self):
return {
"make": self.make,
"model": self.model,
}
23 changes: 23 additions & 0 deletions python-mixins-course/as_dict_mixin_final.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class AsDictMixin:
def as_dict(self):
return vars(self)


class Person(AsDictMixin):
def __init__(self, name, age):
self.name = name
self.age = age


john = Person("John", 42)
print(john.as_dict())


class Car(AsDictMixin):
def __init__(self, make, model):
self.make = make
self.model = model


toyota = Car("Toyota", "Prius")
print(toyota.as_dict())
11 changes: 11 additions & 0 deletions python-mixins-course/sequence.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from abc import ABC, abstractmethod


class Sequence(ABC):
@abstractmethod
def __getitem__(self, index):
pass

@abstractmethod
def __len__(self):
pass
35 changes: 35 additions & 0 deletions python-mixins-course/sequence_abc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from abc import ABC, abstractmethod


class Sequence(ABC):
@abstractmethod
def __getitem__(self, index):
pass

@abstractmethod
def __len__(self):
pass

def __iter__(self):
index = 0
while index < len(self):
yield self[index]
index += 1


class MyRange(Sequence):
def __init__(self, stop):
self.stop = stop # Assume that `stop` is >= 0.

def __getitem__(self, index):
if 0 <= index < self.stop:
return index
raise IndexError

def __len__(self):
return self.stop


r = MyRange(10)
for number in r:
print(number)
35 changes: 35 additions & 0 deletions python-mixins-course/sequence_final.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
from abc import ABC, abstractmethod


class Sequence(ABC):
@abstractmethod
def __getitem__(self, index):
pass

@abstractmethod
def __len__(self):
pass

def __iter__(self):
index = 0
while index < len(self):
yield self[index]
index += 1


class MyRange(Sequence):
def __init__(self, stop):
self.stop = stop # Assume that `stop` is >= 0.

def __getitem__(self, index):
if 0 <= index < self.stop:
return index
raise IndexError

def __len__(self):
return self.stop


r = MyRange(10)
for number in r:
print(number)
40 changes: 40 additions & 0 deletions python-mixins-course/sequence_mixin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from abc import ABC, abstractmethod


class IterMixin:
# __iter__ works IF the class inheriting the mixin
# already provides __getitem__ and __len__.
def __iter__(self):
print("Hey from inside IterMixin.__iter__.")
index = 0
while index < len(self):
yield self[index]
index += 1


class Sequence(IterMixin, ABC):
@abstractmethod
def __getitem__(self, index):
pass

@abstractmethod
def __len__(self):
pass


class MyRange(Sequence):
def __init__(self, stop):
self.stop = stop # Assume that `stop` is >= 0.

def __getitem__(self, index):
if 0 <= index < self.stop:
return index
raise IndexError

def __len__(self):
return self.stop


r = MyRange(10)
for number in r:
print(number)
Loading