Lesser‑Known Python Standard Library Features and Techniques
This article explores several lesser‑known Python standard‑library techniques—including string sanitization, iterator slicing with itertools.islice, skipping initial lines, enforcing keyword‑only arguments, creating custom context managers, using __slots__ for memory savings, limiting CPU/memory via the resource module, controlling module exports with __all__, and simplifying comparisons with functools.total_ordering.
Many tutorials cover popular Python tricks such as unpacking, partial functions, and enumerations, but there are numerous useful features that are rarely discussed. This article shares a collection of such lesser‑known techniques.
Organizing string input – Cleaning user input is a common task. Simple lower‑casing or upper‑casing often suffices, but for more complex cases you can replace whitespace characters (e.g., converting "\n" and "\t" to a single space and removing "\r") and use the unicodedata module with combining() to strip diacritics.
Slicing iterators – Direct slicing of a generator raises TypeError . The solution is to use itertools.islice , which creates an iterator that yields the desired slice while consuming items up to the start of the slice.
Skipping the start of an iterable – When a file begins with a variable number of unwanted lines (e.g., comments), itertools.islice can be used to skip an unknown number of initial items, producing only the lines after the unwanted section.
Functions with keyword‑only arguments – Placing a solitary * before parameters forces all following arguments to be passed by keyword, improving clarity and preventing positional misuse.
Creating objects that support the with statement – Implement custom context managers by defining __enter__ and __exit__ methods, or more simply by using the contextmanager decorator from contextlib , which turns a generator into a context manager.
Using __slots__ to save memory – Declaring __slots__ replaces the per‑instance __dict__ with a fixed‑size array, dramatically reducing memory usage for large numbers of objects. The trade‑off is that new attributes cannot be added and multiple inheritance is prohibited.
Limiting CPU and memory usage – The resource module allows setting soft and hard limits for CPU time ( RLIMIT_CPU ) and memory. After setting limits, a signal handler can terminate the program if the CPU time is exceeded.
Controlling what can be imported – Unlike languages such as Go, Python exports all names by default. Defining an __all__ list restricts what is exported when using from module import * , and an empty __all__ prevents any name from being exported.
Simplifying comparison operators – Implementing all rich comparison methods ( __lt__ , __le__ , __gt__ , __ge__ ) is tedious. The functools.total_ordering decorator fills in the missing methods when you define at least __eq__ and one ordering method.
Conclusion – Not every feature is required for everyday Python programming, but many of these lesser‑known tools can simplify complex or repetitive tasks. When implementing functionality, first check the standard library before turning to third‑party packages.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.