-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoop.py
51 lines (38 loc) · 1.13 KB
/
oop.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
# my postgres dummy
postgres = []
class Database:
# convention: init all used members in constructor
def __init__(self, user, password):
self.db = None
self.user = user
self.password = password
def open_connection(self, user, password):
self.db = postgres.openConnection(user, password)
def insert(self, sql):
self.db.execute(sql)
def close(self):
self.db.close()
@staticmethod
def f():
pass
# pure functions: no state, no side-effects, idempotent
# classmethod: factory method which can be overridden
@classmethod
def create(cls, user, password):
print "classmethod in Database"
return cls(user, password)
class Database2(Database):
@classmethod
def create(cls, user, password):
print "classmethod in Database2"
if not Database2.is_blacklisted(user):
return cls(user, password)
else:
None
@staticmethod
def is_blacklisted(user):
pass
db = Database("bla", "fasel")
Database.f()
db2 = Database.create("bla", "fasel")
db2 = Database2.create("bla", "fasel")