Skip to content

Commit 6f0982b

Browse files
committed
Add classes_and_objects_exercise1.py: demonstrate mypy type checking
- Create Person class with type-annotated __init__ method - Add name (str), age (int), and preferred_operating_system (str) attributes - Create example instances (imran and eliza) - Intentionally access undefined 'address' attribute to demonstrate mypy error detection - Document the mypy error and explain the missing attribute issue
1 parent 1aca968 commit 6f0982b

1 file changed

Lines changed: 17 additions & 0 deletions

File tree

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
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+
imran = Person("Imran", 22, "Ubuntu")
8+
print(imran.name)
9+
print(imran.address)
10+
11+
eliza = Person("Eliza", 34, "Arch Linux")
12+
print(eliza.name)
13+
print(eliza.address)
14+
15+
# Answer
16+
# The error returned after running mypy classes_and_objects_exercise1.py means that the Person class does not have an address attribute.
17+
# An instance of Person was created with name, age and preferred_operating_system only.

0 commit comments

Comments
 (0)