Mastering Python Functions: Write Them Like This or Get Your Pull Request Rejected!
Elshad Karimov
Founder of AppMillers | ERP Expert | Teaching more than 200k people How to Code.
This is the approach we followed at work, and I’m fully on board with it.
The Way We Were Taught to Write Python Functions
Here’s a basic function:
def add_all(num_list, num):
output = []
for n in num_list:
output.append(n + num)
return output
x = add_all([3, 4, 5], 10)
print(x) # Output: [13, 14, 15]
The Problem
At first glance, this function doesn’t tell us:
Sure, after reading the code, we can guess that num_list is a list of numbers, and num is a number, but it’s not immediately clear. In a production-grade app with thousands of functions, wasting time on such guesses adds up.
Type Annotations to the Rescue
This is where type annotations (also called type hints) come in handy. Let’s improve the function by adding them.
from typing import List, Union
def add_all(
num_list: List[Union[int, float]],
num: Union[int, float]
) -> List[Union[int, float]]:
"""
Adds a number to each element in a list.
Args:
num_list: A list of numbers (integers or floats).
num: A number (integer or float).
Returns:
A new list with num added to each element.
"""
output = [n + num for n in num_list]
return output
x: List[Union[int, float]] = add_all([3, 4, 5], 10)
print(x) # Output: [13, 14, 15]
Breaking it Down
Why Tech Lead Insists on This:
Common Type Hints
Here’s a quick guide to commonly used type hints:
# Basics:
a: int = 5
b: float = 3.14
c: bool = True
d: str = 'apple'
# Collections:
from typing import List, Dict, Tuple, Set
# List of integers
a: List[int] = [1, 2, 3]
# Dictionary with string keys and integer values
c: Dict[str, int] = {'apple': 4, 'orange': 5}
You can also use Union for mixed data types:
领英推荐
from typing import Union, Dict
def add10(number: Union[int, float]):
pass
def test(d: Dict[str, Union[int, float, bool]]):
pass
Alternatively, use the cleaner int | float syntax:
def add10(number: int | float):
pass
def test(d: Dict[str, int | float | bool]):
pass
Optional Variables
Sometimes, a variable can be None:
from typing import Optional
from random import random
def test() -> Optional[int]:
if random() > 0.5:
return 1000
return None
Conclusion
If your code lacks type annotations or docstrings, expect your pull request to get rejected. Clear code is not just good practice — it’s essential for maintainability.
If you wish to support me as a creator
?? Special Offer for My Readers ??
Use code: LEARNWITHME at checkout on Udemy to get 90% off my courses.
Thank you for your support — it truly means a lot!
Connect with me: