8000 Added a couple comments and an example of a property setter. · aws096/complete-python-course@e484d25 · GitHub
[go: up one dir, main page]

Skip to content

Commit e484d25

Browse files
committed
Added a couple comments and an example of a property setter.
1 parent 936afaa commit e484d25

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class Saveable(metaclass=ABCMeta):
66
def save(self):
77
Database.insert(self.to_dict())
88

9-
@abstractmethod
9+
# @classmethod (or @staticmethod, or @property)
10+
@abstractmethod # @abstractmethod must always be the innermost decorator if used in conjunction with other decorators.
1011
def to_dict():
1112
pass
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
from flight import Segment, Flight
2+
3+
4+
seg = [Segment('EDI', 'LHR'), Segment('LHR', 'CAN')]
5+
flight = Flight(seg)
6+
7+
print(flight.departure_point)
8+
print(flight)
9+
10+
flight.departure_point = 'GLA'
11+
print('...Set departure to GLA...')
12+
13+
print(flight.departure_point)
14+
print(flight)
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class Flight:
2+
def __init__(self, segments):
3+
"""
4+
CB26 Creates a new Flight wrapper object from an arbitrary number of segments.
5+
6+
:param segments: a list of segments in this flight—normally just one.
7+
"""
8+
self.segments = segments
9+
10+
def __repr__(self):
11+
stops = [self.segments[0].departure, self.segments[0].destination]
12+
for seg in self.segments[1:]:
13+
stops.append(seg.destination)
14+
15+
return ' -> '.join(stops)
16+
17+
@property
18+
def departure_point(self):
19+
return self.segments[0].departure
20+
21+
@departure_point.setter
22+
def departure_point(self, val):
23+
dest = self.segments[0].destination
24+
self.segments[0] = Segment(departure=val, destination=dest)
25+
26+
27+
class Segment:
28+
def __init__(self, departure, destination):
29+
self.departure = departure
30+
self.destination = destination

0 commit comments

Comments
 (0)
0