- Avoid unnecessary
else
statements:
Good example:
if isinstance(other, (int, float)):
return "Valid number!"
return "Invalid number!"
Bad example:
if isinstance(other, (int, float)):
return "Valid number!"
else:
return "Invalid number!"
- Use proper type annotations for function arguments and return types:
Good example:
def add_distance(
dist1: Distance | int | float,
dist2: Distance | int | float
) -> Distance:
return dist1 + dist2
Another good example:
from typing import Union
def add_distance(
dist1: Union[Distance, int, float],
dist2: Union[Distance, int, float]
) -> Distance:
return dist1 + dist2
Bad examples:
def add_distance(
dist1: "Distance",
dist2: (int, float, "Distance")
) -> "Distance":
return dist1 + dist2
- Fix
NameError: name 'Distance' is not defined
due to annotations:
If you encounter a NameError
due to annotations, you can add the following import at the top of your file:
from __future__ import annotations