
Picture by Writer | Ideogram
Python’s normal library has a number of utilities that may rework your code from clunky and verbose to elegant and environment friendly. Amongst these, the functools and itertools modules typically are available tremendous useful for non-trivial duties.
As we speak, we’ll take a look at seven important instruments — capabilities and interior decorators — from these modules that’ll make your Python code higher.
Let’s get began.
🔗 Hyperlink to the code on GitHub
1. functools.lru_cache
You should utilize the @lru_cache
decorator to cache perform outcomes, and to keep away from repeating costly operations.
Right here’s an instance:
from functools import lru_cache
@lru_cache(maxsize=128)
def fetch_user_data(user_id):
# Costly database name
return database.get_user(user_id)
# First name hits database, subsequent calls use cache
person = fetch_user_data(123) # Database name
person = fetch_user_data(123) # Returns cached end result
The way it works: The @lru_cache
decorator shops leads to reminiscence. When fetch_user_data(123)
known as once more, it returns the cached end result as an alternative of hitting the database. maxsize=128
retains the 128 most up-to-date outcomes.
2. itertools.chain
To course of a number of iterables as one steady stream, you should use chain.from_iterable()
from the itertools module.
Let’s take an instance:
from itertools import chain
# Course of a number of log recordsdata as one stream
error_logs = ['app.log', 'db.log', 'api.log']
all_lines = chain.from_iterable(open(f) for f in error_logs)
error_count = sum(1 for line in all_lines if 'ERROR' in line)
The way it works: chain.from_iterable()
takes a number of iterables and creates one steady stream. It reads one line at a time.
3. functools.partial
Partial capabilities in Python are tremendous useful when you should create specialised variations of capabilities. Which means you’d prefer to create variations of the perform with some arguments already set utilizing partial
from the functools module.
This is an instance of a partial perform:
from functools import partial
import logging
def log_event(stage, part, message):
logging.log(stage, f"[{component}] {message}")
# Create specialised loggers
auth_error = partial(log_event, logging.ERROR, 'AUTH')
db_info = partial(log_event, logging.INFO, 'DATABASE')
# Clear utilization
auth_error("Login failed for person")
db_info("Connection established")
The way it works: partial
creates a brand new perform with some arguments pre-filled. Within the instance, auth_error
is actually log_event
with stage and part already set, so that you solely want to supply the message.
4. itertools.mixtures
When you should generate all attainable mixtures of things for testing or optimization, you should use mixtures
from the itertools module.
Think about the next instance:
from itertools import mixtures
options = ['cache', 'compression', 'cdn']
# Take a look at all pairs of options
for combo in mixtures(options, 2):
efficiency = test_feature_combo(combo)
print(f"{combo}: {efficiency}ms")
The way it works: mixtures(options, 2)
generates all attainable pairs from the record. It creates mixtures on-demand with out storing all of them in reminiscence, making it environment friendly for big datasets.
5. functools.singledispatch
The @singledispatch
decorator from the functools module will help you make capabilities that act in another way based mostly on enter sort.
Take a look at the next code snippet:
from functools import singledispatch
from datetime import datetime
@singledispatch
def format_data(worth):
return str(worth) # Default
@format_data.register(datetime)
def _(worth):
return worth.strftime("%Y-%m-%d")
@format_data.register(record)
def _(worth):
return ", ".be part of(str(merchandise) for merchandise in worth)
# Mechanically picks the correct formatter
print(format_data(datetime.now())) # this outputs "2025-06-27"
print(format_data([1, 2, 3])) # this outputs "1, 2, 3"
The way it works: Python checks the kind of the primary argument and calls the suitable registered perform. Nonetheless, it makes use of the default @singledispatch
perform if no particular handler exists.
6. itertools.groupby
You possibly can group consecutive parts that share the identical property utilizing the groupby
perform from itertools.
Think about this instance:
from itertools import groupby
transactions = [
{'type': 'credit', 'amount': 100},
{'type': 'credit', 'amount': 50},
{'type': 'debit', 'amount': 75},
{'type': 'debit', 'amount': 25}
]
# Group by transaction sort
for trans_type, group in groupby(transactions, key=lambda x: x['type']):
complete = sum(merchandise['amount'] for merchandise in group)
print(f"{trans_type}: ${complete}")
The way it works: groupby
teams consecutive objects with the identical key. It returns pairs of (key, group_iterator)
. Essential: it solely teams adjoining objects, so kind your knowledge first if wanted.
7. functools.cut back
You should utilize the cut back
perform from the functools module to use a perform cumulatively to all parts in an iterable to get a single worth.
Take the next instance:
from functools import cut back
# Calculate compound curiosity
monthly_rates = [1.01, 1.02, 0.99, 1.015] # Month-to-month progress charges
final_amount = cut back(lambda complete, price: complete * price, monthly_rates, 1000)
print(f"Ultimate quantity: ${final_amount:.2f}")
The way it works: cut back
takes a perform and applies it step-by-step: first to the preliminary worth (1000) and the primary price, then to that end result and the second price, and so forth. It really works effectively for operations that construct up state.
Wrapping Up
To sum up, we’ve seen how you should use:
@lru_cache
when you may have capabilities which might be referred to as typically with the identical argumentsitertools.chain
when you should course of a number of knowledge sources as one steady streamfunctools.partial
to create specialised variations of generic capabilitiesitertools.mixtures
for systematic exploration of prospects@singledispatch
while you want type-based perform habitsgroupby
for environment friendly consecutive grouping operationscut back
for complicated aggregations that construct up state
The following time you end up writing verbose loops or repetitive code, pause and take into account whether or not certainly one of these may present a extra elegant resolution.
These are only a handful of instruments I discover useful. There are lots of extra in case you take a better take a look at the Python normal library. So yeah, joyful exploring!
Bala Priya C is a developer and technical author from India. She likes working on the intersection of math, programming, knowledge science, and content material creation. Her areas of curiosity and experience embrace DevOps, knowledge science, and pure language processing. She enjoys studying, writing, coding, and low! At the moment, she’s engaged on studying and sharing her information with the developer group by authoring tutorials, how-to guides, opinion items, and extra. Bala additionally creates partaking useful resource overviews and coding tutorials.