generated from CodeYourFuture/Module-Template
-
-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy pathclasses_and_objects.py
More file actions
27 lines (19 loc) · 874 Bytes
/
classes_and_objects.py
File metadata and controls
27 lines (19 loc) · 874 Bytes
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
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")
eliza = Person("Eliza", 34, "Arch Linux")
print(imran.name)
# print(imran.address) # mypy error: Person has no attribute "address"
print(eliza.name)
# print(eliza.address) # mypy error again
def is_adult(person: Person) -> bool:
return person.age >= 18
print(is_adult(imran)) # no mypy error
def print_address(person: Person) -> None:
print(person.address) # mypy will catch this too
# I learned that a class defines what attributes each object will have.
# Mypy can check if I try to access an attribute that doesn't exist.
# It helps to avoid mistakes like typing person.addres instead of person.address.