Fundamentals 10 min read

Useful Python Tricks and Techniques for String Handling, Iterators, Context Managers, Memory Optimization, and More

This article introduces a collection of lesser‑known Python tricks—including string cleaning with translate, iterator slicing via itertools.islice, skipping iterable headers, keyword‑only arguments, custom context managers, memory saving with __slots__, resource‑based CPU/memory limits, controlling module exports, and simplifying comparisons with total_ordering.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Useful Python Tricks and Techniques for String Handling, Iterators, Context Managers, Memory Optimization, and More

Python offers many powerful features, but many useful small tricks are often overlooked. This article presents several practical techniques that can simplify everyday coding tasks.

Cleaning String Input

Using str.translate with a character map can 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>

The same idea can be extended with unicodedata for larger remapping tables.

Iterator Slicing

Since generators do not support slicing, itertools.islice can be used to obtain a slice of an iterator.

<code>import itertools
s = itertools.islice(range(50), 10, 20)
for val in s:
    ...
</code>

islice consumes items up to the start index and then yields the requested range.

Skipping the Beginning of an Iterable

itertools.dropwhile can discard leading lines that match a condition, such as comment lines starting with // .

<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

Placing a * before parameters forces them to be passed by keyword, preventing accidental positional arguments.

<code>def test(*, a, b):
    pass

test(a="value for a", b="value for b")  # 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 __enter__(self):
        # Initialize connection...
        return self
    def __exit__(self, type, value, traceback):
        # Close connection...
        pass

with Connection() as c:
    # Use the connection
    ...

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__ removes the per‑instance __dict__ , 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

Using the resource module together with signal allows setting 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

Define __all__ to limit which names 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__ .

<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))
</code>

While not every technique is essential for daily Python development, they can simplify otherwise verbose tasks and are all part of the standard library.

Memory OptimizationIteratorsContext Managerstring-manipulationresource limits
Python Programming Learning Circle
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.