Skip to content

Commit 093032b

Browse files
Complete exercises for Module Tools/Sprint 5/Prep - step 3 'Classes and objects'
1 parent 5e0ebd9 commit 093032b

1 file changed

Lines changed: 36 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
class Person:
2+
def __init__(self, name: str, age: int, preferred_operating_system: str):
3+
self.name = name
4+
self.age = age
5+
self.preferred_operating_system = preferred_operating_system
6+
7+
8+
9+
imran = Person("Imran", 22, "Ubuntu")
10+
print(imran.name)
11+
# print(imran.address) # mypy: Person has no attribute address
12+
13+
14+
eliza = Person("Eliza", 34, "Arch Linux")
15+
print(eliza.name)
16+
# print(eliza.address) # mypy: Person has no attribute address
17+
18+
19+
def is_adult(person: Person) -> bool:
20+
return person.age >= 18
21+
22+
23+
print(is_adult(imran))
24+
25+
26+
# ------------------------
27+
# Exercise 1
28+
# ------------------------
29+
# Write a new function in the file that accepts a Person as a parameter and tries to access
30+
# a property that doesn’t exist. Run it through mypy and check that it does report an error.
31+
32+
def is_located_in(person: Person) -> bool:
33+
return person.is_located_in == "Manchester"
34+
35+
print(is_located_in(eliza)) # prints: AttributeError: 'Person' object has no attribute 'is_located_in'
36+

0 commit comments

Comments
 (0)