Useful Python Tricks and Advanced Techniques
This article presents a collection of lesser‑known Python tricks—including string cleaning with translate, iterator slicing via itertools.islice, skipping header lines, keyword‑only functions, custom context managers, memory‑saving __slots__, resource limits, import control, and total_ordering—to help developers write cleaner, more efficient code.
Python offers many powerful features that are often overlooked; this article introduces several practical tricks that can simplify common programming tasks.
String cleaning : Using str.translate with a character map to replace newline, tab, and carriage‑return characters with spaces or remove them entirely.
<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 : The itertools.islice function can create a slice of any iterator without raising TypeError .
<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 : itertools.dropwhile can discard leading lines (e.g., comment lines) before processing the rest of a file.
<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>Keyword‑only arguments (kwargs) : Defining a function with a leading * forces all following parameters to be passed by keyword.
<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, or use contextlib.contextmanager for a simpler approach.
<code>class Connection:
def __init__(self):
...
def __enter__(self):
# Initialize connection...
pass
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, reducing memory usage at the cost of flexibility.
<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 : The resource and signal modules can set hard limits for CPU time and memory allocation.
<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 : Using the __all__ list restricts which symbols are exported from a module.
<code>def foo():
pass
def bar():
pass
__all__ = ["bar"]</code>Simplifying comparison operators : The functools.total_ordering decorator generates the full set of rich comparison methods from __lt__ and __eq__ implementations.
<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>While not every feature shown is required for everyday Python development, each can be handy in specific scenarios and help reduce boilerplate or improve performance, and all are part of the Python standard library.
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.