Skip to content

Commit fbb3825

Browse files
committed
Completed "Methods" exercises.
1 parent 23329bf commit fbb3825

2 files changed

Lines changed: 23 additions & 0 deletions

File tree

prep exercises/Methods exercise 1

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
The advantage of a method as a opposed to freestanding function is that it works on the instance of a type.
2+
The freestanding function knows nothing about the instance of the type because it doesn't belong to a type.
3+
On an instance, the method can influence the internal states.
4+
However freestanding function can only work on public APIs whereas instance methods have access to internal APIs.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from datetime import date
2+
3+
class Person:
4+
def __init__(self, name: str, date_of_birth: date, preferred_operating_system: str):
5+
self.name = name
6+
self.date_of_birth = date_of_birth
7+
self.preferred_operating_system = preferred_operating_system
8+
9+
def is_adult(self):
10+
today = date.today()
11+
age = today.year - self.date_of_birth.year
12+
13+
if (today.month, today.day) < (self.date_of_birth.month, self.date_of_birth.day):
14+
age -= 1
15+
16+
return age >= 18
17+
18+
imran = Person("Imran", date(2004, 6, 28), "Ubuntu")
19+
print(imran.is_adult())

0 commit comments

Comments
 (0)