Skip to content

Commit e1f9545

Browse files
committed
classes and objects task
1 parent 732a40b commit e1f9545

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

sprint-5/classes_and_objects.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Exercise 1 — Save the above code to a file, and run it through mypy.
2+
#Read the error, and make sure you understand what it’s telling you.
3+
4+
class Person:
5+
def __init__(self, name: str, age: int, preferred_operating_system: str):
6+
self.name = name
7+
self.age = age
8+
self.preferred_operating_system = preferred_operating_system
9+
10+
imran = Person("Imran", 22, "Ubuntu")
11+
print(imran.name)
12+
print(imran.address) # mypy error: "Person" has no attribute "address"
13+
14+
eliza = Person("Eliza", 34, "Arch Linux")
15+
print(eliza.name)
16+
print(eliza.address) # mypy error: "Person" has no attribute "address"
17+
18+
# Exercise 2 — Add the is_adult code to the file you saved earlier.
19+
20+
# Run it through mypy - notice that no errors are reported -
21+
# mypy understands that Person has a property named age so is happy with the function.
22+
23+
# Write a new function in the file that accepts a Person as a
24+
# parameter and tries to access a property that doesn’t exist. Run it through mypy and check that it does report an error.
25+
def is_adult(person: Person) -> bool:
26+
return person.age >= 18
27+
28+
print(is_adult(imran)) # True
29+
30+
def get_favorite_food(person: Person) -> str:
31+
return person.favorite_food # mypy error: "Person" has no attribute "favorite_food"
32+

0 commit comments

Comments
 (0)