-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathclasses_and_objects.py
More file actions
37 lines (24 loc) · 1.13 KB
/
classes_and_objects.py
File metadata and controls
37 lines (24 loc) · 1.13 KB
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
# ------------------------
# Q. Read the error, and make sure you understand what it’s telling you.
# ------------------------
class Person:
def __init__(self, name: str, age: int, preferred_operating_system: str):
self.name = name
self.age = age
self.preferred_operating_system = preferred_operating_system
imran = Person("Imran", 22, "Ubuntu")
print(imran.name)
# print(imran.address)
eliza = Person("Eliza", 34, "Arch Linux")
print(eliza.name)
# print(eliza.address)
# A. The error is telling me that the 'address' attribute cannot be accessed because it does not exist on the 'Person' class
def is_adult(person: Person) -> bool:
return person.age >= 18
print(is_adult(imran))
# ------------------------
# Q. Write a new function in the file that accepts a Person as a parameter and tries to access a property that doesn’t exist. Run it through mypy and check that it does report an error.
# ------------------------
# def get_address(person: Person) -> str:
# return person.address
# A. As in the previous example, the attribute cannot be accessed because it does not exist on the 'Person' class