Advanced Python Features You May Not Know
This article introduces a collection of lesser‑known but useful Python standard‑library features—including string cleaning, iterator slicing, skipping iterable headers, keyword‑only functions, context‑manager creation, __slots__ memory savings, resource limits, import control, and simplified comparison operators—explaining their usage and benefits for everyday programming.
Cleaning String Input
Cleaning user input is relevant to almost every program; converting characters to lower‑ or upper‑case often suffices, but for more complex cases you can replace whitespace characters ("\n", "\t") with a single space and remove "\r", or use the unicodedata package with its combining() function to strip all accents.
Slicing Iterators
Attempting to slice a generator raises a TypeError because generators are not subscriptable. The simple solution is to use itertools.islice , which creates an iterator that yields the desired slice while consuming all items up to the start point.
Skipping the Beginning of an Iterable
When a file starts with an unwanted variable number of lines (e.g., comments), itertools provides an easy way to drop those initial items without knowing their count, allowing you to process only the relevant part of the iterable.
Functions with Only Keyword Arguments (kwargs)
Defining functions that accept only keyword arguments improves clarity; this is achieved by placing a solitary * before the keyword parameters. Positional arguments placed before the * remain valid.
Creating Objects that Support the with Statement
Custom context managers can be built by implementing __enter__ and __exit__ methods, or more concisely using the contextlib.contextmanager decorator, which turns a generator into a proper with‑statement manager.
Using __slots__ to Save Memory
When many instances of a class are created, memory usage can balloon because each instance stores its attributes in a dictionary. Declaring __slots__ replaces the per‑instance dict with a fixed‑size array, dramatically reducing memory consumption, though it disallows adding new attributes and prevents multiple inheritance.
Limiting CPU and Memory Usage
The resource module lets you set soft and hard limits for CPU time (RLIMIT_CPU) and memory (RLIMIT_AS). After setting the limits, a signal handler can terminate the program if the CPU time exceeds the bound.
Controlling What Can Be Imported
Unlike languages such as Go, Python exports everything by default. By defining an __all__ list you can explicitly specify which names are public; an empty __all__ makes the module export nothing, causing AttributeError on import.
Simplified Implementation of Comparison Operators
Implementing all rich comparison methods ( __lt__ , __le__ , __gt__ , __ge__ ) is tedious. The functools.total_ordering decorator requires only __lt__ and __eq__ , automatically providing the remaining methods.
Conclusion
Not every feature described is essential for daily Python work, but many can simplify otherwise verbose tasks. All of these utilities belong to the Python standard library, so you should first search there 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.