-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExercise-35-Greeter.py
57 lines (40 loc) · 1.36 KB
/
Exercise-35-Greeter.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
54
55
56
57
# Class with somewhat unrelated methods
from datetime import datetime
class Greeter:
def __init__(self, name):
self.name = name
def _day(self): # <1>
return datetime.now().strftime('%A')
def _part_of_day(self): # <2>
current_hour = datetime.now().hour
if current_hour < 12:
part_of_day = 'morning'
elif 12 <= current_hour < 17:
part_of_day = 'afternoon'
else:
part_of_day = 'evening'
return part_of_day
def greet(self, store): # <3>
print(f'Hi, my name is {self.name}, and welcome to {store}!')
print(f'How\'s your {self._day()} {self._part_of_day()} going?')
print('Here\'s a coupon for 20% off!')
...
# Methods extracted as functions outside the class
def day():
return datetime.now().strftime('%A')
def part_of_day():
current_hour = datetime.now().hour
if current_hour < 12:
part_of_day = 'morning'
elif 12 <= current_hour < 17:
part_of_day = 'afternoon'
else:
part_of_day = 'evening'
return part_of_day
# Referencing extracted functions inside the class
class Greeter:
...
def greet(self, store):
print(f'Hi, my name is {self.name}, and welcome to {store}!')
print(f'How\'s your {day()} {part_of_day()} going?')
print('Here\'s a coupon for 20% off!')