-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinheritance.py
More file actions
39 lines (29 loc) · 926 Bytes
/
inheritance.py
File metadata and controls
39 lines (29 loc) · 926 Bytes
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
class Device:
def __init__(self, name, connected_by):
self.name = name
self.connected_by = connected_by
self.connected = True
def __str__(self):
return f"Device {self.name!r} ({self.connected_by})"
def disconnect(self):
self.connected = False
print("disconnected")
class Printer(Device):
def __init__(self, name, connected_by, capacity):
super().__init__(name, connected_by)
self.capacity = capacity
self.remaining_pages = capacity
def print_pages(self, pages):
if not self.connected:
print("The printer is not connected.")
else:
print(f"printing {pages} pages.")
self.remaining_pages -= pages
def __str__(self):
return f"{super().__str__()} with {self.remaining_pages} remaining pages."
printer = Printer("Sony", "USB", 500)
print(printer)
printer.print_pages(200)
print(printer)
printer.disconnect()
printer.print_pages(20)