Lesser‑Known Python Tricks and Techniques
This article introduces a collection of lesser‑known Python tricks—including string cleaning with translate, iterator slicing via itertools, skipping header lines, keyword‑only functions, custom context managers, memory‑saving __slots__, resource limits, import control with __all__, and total_ordering—to help developers write cleaner, more efficient code.
Python provides many powerful features, yet numerous handy tricks remain under‑documented. This article gathers several lesser‑known techniques that can simplify everyday coding tasks.
String Input Cleanup
Often you only need to convert characters to lower/upper case, but for complex whitespace handling you can build a translation map and use str.translate to replace newlines, tabs, and carriage returns.
<code>user_input = "This\nstring has\tsome whitespaces...\r\n"
character_map = {
ord('\n'): ' ',
ord('\t'): ' ',
ord('\r'): None,
}
print(user_input.translate(character_map)) # This string has some whitespaces... </code>The example shows \n and \t replaced by a single space and \r removed.
Iterator Slicing (Slice)
Direct slicing of a generator raises 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 0x...>
for val in s:
...</code>islice consumes items up to the start index, then yields the specified slice.
Skipping the Beginning of an Iterable
When a file starts with unwanted comment lines, itertools.dropwhile can discard them without knowing their count.
<code>string_from_file = """
// Author: ...
// License: ...
//
// Date: ...
Actual content...
"""
import itertools
for line in itertools.dropwhile(lambda l: l.startswith("//"), string_from_file.split("\n")):
print(line)</code>Keyword‑Only Functions (kwargs)
Defining a function with a leading * forces all arguments 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="value2") # Works</code>Creating Objects that Support the with Statement
Implement the context‑manager protocol by defining __enter__ and __exit__ methods.
<code>class Connection:
def __init__(self):
...
def __enter__(self):
# Initialize connection...
return self
def __exit__(self, type, value, traceback):
# Close connection...
...
with Connection() as c:
# __enter__() runs
...
# __exit__() runs</code>A shorter alternative uses contextlib.contextmanager :
<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, at the cost of flexibility 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
Use the resource module together with signal to set hard limits on CPU time and memory consumption.
<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
Python exports all module members by default; defining __all__ restricts what from module import * brings in.
<code>def foo():
pass
def bar():
pass
__all__ = ["bar"]</code>Simplifying Comparison Operators with functools.total_ordering
Implementing only __lt__ and __eq__ and decorating the class with total_ordering automatically provides the full set of rich comparisons.
<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>These techniques are all part of Python’s standard library and can be combined to write more concise, efficient, and maintainable code.
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.