8000 Add another example of interface usage in app.py · coding794/complete-python-course@958a046 · GitHub
[go: up one dir, main page]

Skip to content

Commit 958a046

Browse files
committed
Add another example of interface usage in app.py
1 parent e484d25 commit 958a046

File tree

1 file changed

+11
-1
lines changed
  • section16/sample_code/4-abc-3-and-interfaces

1 file changed

+11
-1
lines changed

section16/sample_code/4-abc-3-and-interfaces/app.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
from database import Database
22
from admin import Admin
3+
from user import User
4+
from saveable import Saveable
35

46
a = Admin('paco', 'perez', 2)
57
b = Admin('rolf', 'smith', 1)
@@ -9,4 +11,12 @@
911

1012
user = Database.find(lambda x: x['username'] == 'paco')[0]
1113
user_obj = Admin(**user)
12-
print(user_obj.username)
14+
print(user_obj.username)
15+
16+
print(isinstance(user_obj, Saveable)) # This is True because it's a subclass
17+
18+
# You can do things like these without worry:
19+
20+
users_to_save = [a, b, User('jose', '1234')]
21+
for u in users_to_save:
22+
u.save() # This is fine, because all users (Admin and User) implement the Saveable interface so we know they have a .save() method

0 commit comments

Comments
 (0)
0