Skip to content

Commit 21fdd0a

Browse files
committed
Add classes_and_objects_exercise2.py: demonstrate mypy type checking
- Create Person class with type-annotated __init__ method - Add name (str), age (int), and preferred_operating_system (str) attributes - Implement is_adult() function that correctly accesses Person.age attribute - Add is_adult_wrong_attribute() function that intentionally accesses non-existent Person.address - Demonstrate mypy error detection for invalid attribute access - Show how mypy validates correct vs incorrect property references
1 parent 6f0982b commit 21fdd0a

1 file changed

Lines changed: 22 additions & 0 deletions

File tree

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
10+
11+
eliza = Person("Eliza", 34, "Arch Linux")
12+
print(eliza.name)
13+
14+
def is_adult(person: Person) -> bool:
15+
return person.age >= 18
16+
17+
print(is_adult(imran))
18+
19+
def is_adult_wrong_attribute(person: Person) -> bool:
20+
return person.address
21+
22+
print(is_adult_wrong_attribute(imran))

0 commit comments

Comments
 (0)