8000 Add examples of multiple inheritance, ABCs, and interfaces. All these… · DORELAS/complete-python-course@936afaa · GitHub
[go: up one dir, main page]

Skip to content

Commit 936afaa

Browse files
committed
Add examples of multiple inheritance, ABCs, and interfaces. All these are used together for the same thing: ensuring a class has particular functionality that another class defines.
1 parent 4407c62 commit 936afaa

File tree

17 files changed

+192
-0
lines changed

17 files changed

+192
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from user import User
2+
from saveable import Saveable
3+
4+
class Admin(User, Saveable):
5+
def __init__(self, username, password, access):
6+
super(Admin, self).__init__(username, password)
7+
self.access = access
8+
9+
def __repr__(self):
10+
return f'<Admin {self.username}, access {self.access}>'
11+
12+
def to_dict(self):
13+
return {
14+
'username': self.username,
15+
'password': self.password,
16+
'access': self.access
17+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from database import Database
2+
from admin import Admin
3+
4+
a = Admin('paco', 'perez', 2)
5+
b = Admin('rolf', 'smith', 1)
6+
7+
a.save()
8+
b.save()
9+
10+
user = Database.find(lambda x: x['username'] == 'paco')[0]
11+
user_obj = Admin(**user)
12+
print(user_obj.username)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Database:
2+
content = {'users': []}
3+
4+
@classmethod
5+
def insert(cls, data):
6+
cls.content['users'].append(data)
7+
8+
@classmethod
9+
def remove(cls, finder):
10+
cls.content['users'] = [user for user in cls.content['users'] if not finder(user)]
11+
12+
@classmethod
13+
def find(cls, finder):
14+
return [user for user in cls.content['users'] if finder(user)]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
from database import Database
2+
3+
class Saveable:
4+
def save(self):
5+
Database.insert(self.to_dict())
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
class User:
2+
def __init__(self, username, password):
3+
self.username = username
4+
self.password = password
5+
6+
def login(self):
7+
return 'Logged in!'
8+
9+
def __repr__(self):
10+
return f'<User {self.username}>'

section16/sample_code/2-abc/animal.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from abc import ABCMeta, abstractmethod
2+
3+
class Animal(metaclass=ABCMeta):
4+
def walk(self):
5+
print('Walking...')
6+
7+
def eat(self):
8+
print('Eating...')
9+
10+
@abstractmethod
11+
def num_legs():
12+
pass

section16/sample_code/2-abc/app.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from animal import Animal
2+
from dog import Dog
3+
4+
d = Dog('Bob')
5+
# a = Animal()
6+
7+
d.walk()

section16/sample_code/2-abc/dog.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from animal import Animal
2+
3+
4+
class Dog(Animal):
5+
def __init__(self, name):
6+
self.name = name
7+
8+
def num_legs(self):
9+
return 4
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from abc import ABCMeta, abstractmethod
2+
3+
class Animal(metaclass=ABCMeta):
4+
def __init__(self, name):
5+
self.name = name
6+
7+
def walk(self):
8+
print('Walking...')
9+
10+
def eat(self):
11+
print('Eating...')
12+
13+
@abstractmethod
14+
def num_legs():
15+
pass

section16/sample_code/3-abc-2/app.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from dog import Dog
2+
from monkey import Monkey
3+
4+
d = Dog('Bob')
5+
m = Monkey('Rolf')
6+
7+
d.walk()
8+
m.eat()

section16/sample_code/3-abc-2/dog.py

Lines changed: 6 10000 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from animal import Animal
2+
3+
4+
class Dog(Animal):
5+
def num_legs(self):
6+
return 4
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from animal import Animal
2+
3+
4+
class Monkey(Animal):
5+
def num_legs(self):
6+
return 2
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from user import User
2+
3+
class Admin(User):
4+
def __init__(self, username, password, access):
5+
super(Admin, self).__init__(username, password)
6+
self.access = access
7+
8+
def __repr__(self):
9+
return f'<Admin {self.username}, access {self.access}>'
10+
11+
def to_dict(self):
12+
return {
13+
'username': self.username,
14+
'password': self.password,
15+
'access': self.access
16+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from database import Database
2+
from admin import Admin
3+
4+
a = Admin('paco', 'perez', 2)
5+
b = Admin('rolf', 'smith', 1)
6+
7+
a.save()
8+
b.save()
9+
10+
user = Database.find(lambda x: x['username'] == 'paco')[0]
11+
user_obj = Admin(**user)
12+
print(user_obj.username)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class Database:
2+
content = {'users': []}
3+
4+
@classmethod
5+
def insert(cls, data):
6+
cls.content['users'].append(data)
7+
8+
@classmethod
9+
def remove(cls, finder):
10+
cls.content['users'] = [user for user in cls.content['users'] if not finder(user)]
11+
12+
@classmethod
13+
def find(cls, finder):
14+
return [user for user in cls.content['users'] if finder(user)]
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from abc import ABCMeta, abstractmethod
2+
3+
from database import Database
4+
5+
class Saveable(metaclass=ABCMeta):
6+
def save(self):
7+
Database.insert(self.to_dict())
8+
9+
@abstractmethod
10+
def to_dict():
11+
pass
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
from saveable import Saveable
2+
3+
class User(Saveable):
4+
def __init__(self, username, password):
5+
self.username = username
6+
self.password = password
7+
8+
def login(self):
9+
return 'Logged in!'
10+
11+
def __repr__(self):
12+
return f'<User {self.username}>'
13+
14+
def to_dict(self):
15+
return {
16+
'username': self.username,
17+
'password': self.password
18+
}

0 commit comments

Comments
 (0)
0