-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy path05.mixins_framework.py
53 lines (40 loc) · 1.18 KB
/
05.mixins_framework.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#Parents classes : Loggable and Database
class Loggable:
def __init__(self):
self.title = ''
def log(self):
print(f'Log message from {self.title}')
class Database:
def __init__(self):
self.server = ''
def connect(self):
print(f'Connecting to database on {self.server}')
#fancy framework which allows to make multiple inheritances
def framework(item):
if isinstance(item, Database):
item.connect()
if isinstance(item, Loggable):
item.log()
#-------------------------------------
#01. Child inheriting from Multiple classes
print('---------------')
class SqlDatabase(Loggable, Database):
def __init__(self):
self.title = 'sql connection demo'
self.server = 'Some Server 456'
sql = SqlDatabase()
framework(sql)
#02. Child inheriting from Loggable only
print('---------------')
class LogOnly(Loggable):
def __init__(self):
self.title = 'Log Only'
log_only = LogOnly()
framework(log_only)
#03. Child inheriting from Database only
print('---------------')
class DBOnly(Database):
def __init__(self):
self.server = 'PostgresSQL Server 123'
db_only = DBOnly()
framework(db_only)