Python tutorials > Core Python Fundamentals > Data Types and Variables > What is type hinting?
What is type hinting?
Type hinting, also known as type annotations, is a feature introduced in Python 3.5 (PEP 484) that allows you to specify the expected data type of variables, function arguments, and return values. It enhances code readability and maintainability by providing static type information to both developers and static analysis tools. While Python remains a dynamically typed language (meaning type checking occurs at runtime), type hints enable you to catch type-related errors earlier in the development process, such as during code review or with the help of static type checkers like MyPy.
Basic Type Hinting Syntax
In the example above:
name: str
indicates that the name
parameter of the greet
function is expected to be a string.-> str
after the parameter list specifies that the greet
function is expected to return a string.age: int = 30
declares that the variable age
should be an integer, and initializes it to 30.pi: float = 3.14159
declares that the variable pi
should be a floating-point number, initialized to 3.14159.
def greet(name: str) -> str:
return f"Hello, {name}!"
age: int = 30
pi: float = 3.14159
Concepts Behind the Snippet
Key concepts demonstrated in the previous snippet include:
Real-Life Use Case Section
In this real-life example, we have a Product
class and a calculate_total
function. Type hints are used to improve clarity and detect potential type errors:
List[Product]
indicates that the products
parameter of calculate_total
is expected to be a list of Product
objects. This requires importing List
from the typing
module.-> float
specifies that calculate_total
should return a float, representing the total price.
from typing import List
class Product:
def __init__(self, name: str, price: float):
self.name = name
self.price = price
def calculate_total(products: List[Product]) -> float:
total = 0.0
for product in products:
total += product.price
return total
products = [Product("Shirt", 25.0), Product("Pants", 50.0)]
total_price = calculate_total(products)
print(f"Total price: ${total_price}")
Best Practices
When using type hinting, consider these best practices:
typing
module: For more complex types like lists, dictionaries, tuples, and custom classes, use the typing
module (e.g., List
, Dict
, Tuple
, Optional
, Union
).
Interview Tip
During interviews, be prepared to discuss the benefits of type hinting, how it improves code quality, and the role of static type checkers. Explain the difference between static and dynamic typing in Python.
When to Use Them
Type hints are most beneficial in:
Memory Footprint
Type hints themselves do not directly affect the memory footprint of your Python code at runtime. They are primarily used for static analysis and are typically ignored by the interpreter during execution.
Alternatives
Alternatives to type hinting include:
isinstance()
checks to verify types at runtime. This can impact performance and is less elegant than static type hinting.
Pros
Advantages of using type hints:
Cons
Disadvantages of using type hints:
FAQ
-
Are type hints enforced at runtime?
No, type hints are not enforced by the Python interpreter at runtime by default. They are primarily for static analysis and documentation. However, you can use libraries like
enforce
or write custom decorators to enforce type hints at runtime if needed. -
What is MyPy?
MyPy is a popular static type checker for Python. It analyzes your code based on type hints and reports any type inconsistencies. It helps you catch potential errors early in the development cycle.
-
Can I use type hints in older versions of Python?
Type hinting was introduced in Python 3.5. While you can use type hints in Python 3.5+, they are not supported in older versions like Python 2. If you need to support older versions, consider using docstrings or runtime type checks.