Useful Python Tricks: String Cleaning, Iterator Slicing, Keyword‑Only Arguments, Context Managers, __slots__, Resource Limits, __all__, and Total Ordering
This article presents a collection of lesser‑known Python tricks—including string normalization, iterator slicing, keyword‑only functions, custom context managers, memory‑saving __slots__, CPU/memory limits, import control with __all__, and simplified ordering with total_ordering—to help developers write cleaner and more efficient code.
Many articles introduce popular Python features such as variable unpacking, partial functions, and enumerations, but numerous useful tricks remain undocumented; this article gathers several of those lesser‑known techniques.
Cleaning String Input
Normalizing user input often involves converting characters to lower or upper case or using regular expressions, but a simple translation table can replace whitespace characters and remove carriage returns.
<code>user_input = "This\nstring has\tsome whitespaces...\r\n"
character_map = {
ord('\n'): ' ',
ord('\t'): ' ',
ord('\r'): None
}
user_input.translate(character_map) # This string has some whitespaces... </code>Iterator Slicing (Slice)
Attempting to slice a generator raises a TypeError ; using itertools.islice creates an iterator that yields the desired range.
<code>import itertools
s = itertools.islice(range(50), 10, 20) # <itertools.islice object at 0x7f70fab88138>
for val in s:
...</code>Skipping the Beginning of an Iterable
When a file starts with unwanted lines (e.g., comments), itertools.dropwhile can discard them without knowing their exact count.
<code>string_from_file = """
// Author: ...
// License: ...
//
// Date: ...
Actual content...
"""
import itertools
for line in itertools.dropwhile(lambda line: line.startswith("//"), string_from_file.split("\n")):
print(line)</code>Functions with Keyword‑Only Arguments (kwargs)
Defining a function with a leading * forces all following parameters to be passed by keyword, improving clarity.
<code>def test(*, a, b):
pass
test("value for a", "value for b") # TypeError
test(a="value", b="value 2") # Works</code>Creating Objects that Support the with Statement
Implement __enter__ and __exit__ methods to define a context manager, or use contextlib.contextmanager for a simpler approach.
<code>class Connection:
def __init__(self):
...
def __enter__(self):
# Initialize connection...
return self
def __exit__(self, type, value, traceback):
# Close connection...
pass
with Connection() as c:
# __enter__() executes
...
# __exit__() executes
</code> <code>from contextlib import contextmanager
@contextmanager
def tag(name):
print(f"<{name}>")
yield
print(f"</{name}>")
with tag("h1"):
print("This is Title.")</code>Saving Memory with __slots__
Defining __slots__ replaces the per‑instance __dict__ with a fixed‑size array, drastically reducing memory usage, though it disallows adding new attributes and multiple inheritance.
<code>class Person:
__slots__ = ["first_name", "last_name", "phone"]
def __init__(self, first_name, last_name, phone):
self.first_name = first_name
self.last_name = last_name
self.phone = phone</code>Limiting CPU and Memory Usage
Python’s resource module can set hard limits on CPU time and memory consumption, raising a signal or exiting when limits are exceeded.
<code>import signal, resource, os
def time_exceeded(signo, frame):
print("CPU exceeded...")
raise SystemExit(1)
def set_max_runtime(seconds):
soft, hard = resource.getrlimit(resource.RLIMIT_CPU)
resource.setrlimit(resource.RLIMIT_CPU, (seconds, hard))
signal.signal(signal.SIGXCPU, time_exceeded)
def set_max_memory(size):
soft, hard = resource.getrlimit(resource.RLIMIT_AS)
resource.setrlimit(resource.RLIMIT_AS, (size, hard))</code>Controlling What Can Be Imported
Unlike languages that hide non‑exported members, Python exports everything unless __all__ is defined; setting __all__ restricts the public API.
<code>def foo():
pass
def bar():
pass
__all__ = ["bar"]</code>Simple Way to Implement Comparison Operators
The functools.total_ordering decorator generates the full set of ordering methods when only __lt__ and __eq__ are provided.
<code>from functools import total_ordering
@total_ordering
class Number:
def __init__(self, value):
self.value = value
def __lt__(self, other):
return self.value < other.value
def __eq__(self, other):
return self.value == other.value
print(Number(20) > Number(3))
print(Number(1) < Number(5))
print(Number(15) >= Number(15))
print(Number(10) <= Number(2))</code>Conclusion
Not every trick shown is essential for everyday Python development, but each can simplify otherwise verbose tasks; all are part of the standard library, so consult the official documentation before seeking third‑party solutions.
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.