Type Hints

Intermediate Object-Oriented Programming in Python

Jake Roach

Data Engineer

Code without type hints

class Student:
    def __init__(self, name, student_id, tuition_balance):
        self.name = name
        self.student_id = student_id
        self.tuition_balance = tuition_balance

# Trying to create a class can be tricky
walker = Student("Sarah Walker", 319921, 15000)
  • Is student_id supposed to be an integer?
  • Should tuition_balance be a float or an integer? Maybe a string?
  • After walker has been created, how can we remember what type it is later in code?
Intermediate Object-Oriented Programming in Python

Type hints

Optional tool that allows for information about an object to be added to code

  • Easier to read, troubleshoot
  • One of the best ways to demonstrate enterprise-grade Python skills
  • Not enforced by the interpreter
  • Built-in type keywords, typing library, custom classes
# Add type hinting when creating a 
# variable
gpa: float = 3.92
# Add type hinting to a function/method 
# definition
def check_grades(year: str) -> List[int]:
    ...
# Can even use custom classes
students: Dict[str, Student] = {...}
Intermediate Object-Oriented Programming in Python

Type hinting with built-in type keywords

# Type hinting for declarative logic
name: str = "Frost"  # Before: name = "Frost"
student_id: int = 91031367
tuition_balance: float = 17452.78
# Type hinting for function/method definitions
def get_schedule(semester: str) -> dict:
    ...
  • Use the syntax variable: type
  • Declarative, signature of function/method
  • def .... () -> type: to specify return type
Intermediate Object-Oriented Programming in Python

typing Library

typing is a library used to provide more tools to type hint

  • List, Dict, Tuple
  • Hint top-level objects, and elements of objects
from typing import List, Dict

student_names: List[str] = ["Morgan", "Chuck", "Anna"]
student_gpas: Dict[str, float] = {
    "Casey": 3.71,
    "Sarah": 4.0
}
  • Any, Set, Iterator, Callable
Intermediate Object-Oriented Programming in Python

Type hinting with custom classes

class Student:
    def __init__(self, name: str, student_id: int, tuition_balance: float) -> None:
        self.name: str = name
        self.student_id: int = student_id
        self.tuition_balance: float = tuition_balance

    def get_course(self, course_id: str) -> Course:
        ...
        return course
# Use Student and Course to type hint
walker: Student = Student("Sarah Walker", 319921, 15000)
data_science: Course = walker.get_course("TDM-20100")
Intermediate Object-Oriented Programming in Python

Checking object types

# walker: Student = Student("Sarah Walker", 319921, 15000)
print(type(walker))
<class '__main__.Student'>
# data_science: Course = walker.get_course("TDM-20100")
print(type(data_science))
<class '__main__.Course'>
Intermediate Object-Oriented Programming in Python

Let's practice!

Intermediate Object-Oriented Programming in Python

Preparing Video For Download...