Fundamentals 10 min read

Unlock Hidden Python Tricks: From String Cleanup to Memory Limits

Explore a collection of lesser‑known Python tricks—including string sanitization, iterator slicing, context‑manager shortcuts, memory‑saving slots, resource limits, controlled imports, and simplified ordering—each illustrated with concise code examples to help you write cleaner, more efficient programs.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Unlock Hidden Python Tricks: From String Cleanup to Memory Limits

This article introduces several Python tips that are often omitted elsewhere, offering practical solutions for everyday coding challenges.

Cleaning Up String Input

Normalizing user input is common; converting case or using regular expressions often suffices, but more complex cases benefit from translation tables.

user_input = "This  
string has  some whitespaces...  
"
character_map = {
    ord(' '): None,
    ord('\t'): None,
    ord('
'): ' '
}
user_input.translate(character_map)  # This string has some whitespaces...

The example replaces spaces, tabs, and removes carriage returns, demonstrating a simple approach that can be extended with the unicodedata module and combining() for larger mappings.

Iterator Slicing

Slicing a generator raises TypeError, but itertools.islice provides a straightforward solution.

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

creates an iterator that yields the desired slice while preserving earlier items in memory.

Skipping the Beginning of an Iterable

When files start with unwanted lines (e.g., comments), itertools.dropwhile can skip them.

string_from_file = """
// Author: ...
// License: ...
// 
// Date: ...
Actual content...
"""
import itertools
for line in itertools.dropwhile(lambda line: line.startswith("//"), string_from_file.split("
")):
    print(line)

This prints only the content after the initial comment block, useful when the length of the header is unknown.

Functions with Keyword‑Only Arguments (kwargs)

Defining functions that accept only keyword arguments clarifies the API.

def test(*, a, b):
    pass
# test("value for a", "value for b")  # TypeError
test(a="value", b="value 2")  # Works

Placing a * before parameters forces them to be passed by name.

Creating Objects that Support the with Statement

Implement the context‑manager protocol using __enter__ and __exit__, or simplify with contextlib.contextmanager.

class Connection:
    def __init__(self):
        ...
    def __enter__(self):
        # Initialize connection...
        return self
    def __exit__(self, exc_type, value, traceback):
        # Close connection...
        pass
with Connection() as c:
    ...
from contextlib import contextmanager
@contextmanager
def tag(name):
    print(f"<{name}>")
    yield
    print(f"</{name}>")
with tag("h1"):
    print("This is Title.")

The decorator handles entry and exit actions automatically.

Saving Memory with __slots__

Using __slots__ replaces the per‑instance __dict__ with a fixed‑size array, reducing memory usage.

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

Note that classes with __slots__ cannot add new attributes dynamically and cannot use multiple inheritance.

Limiting CPU and Memory Usage

Python’s resource and signal modules allow setting hard limits.

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

The functions configure both CPU time and memory consumption limits, terminating the program if limits are exceeded.

Controlling What Gets Imported

Python exports all module members by default; defining __all__ restricts this.

def foo():
    pass
def bar():
    pass
__all__ = ["bar"]

Only bar is exported; an empty __all__ prevents any export, causing AttributeError on import.

Simplifying Comparison Operators with functools.total_ordering

Implementing all rich comparison methods is tedious; total_ordering generates the missing ones from __lt__ and __eq__.

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

The decorator fills in the remaining comparison methods, reducing boilerplate.

Conclusion

While not every feature discussed is essential for daily Python development, many can simplify repetitive or cumbersome tasks. All demonstrated functionalities belong to Python’s standard library; if a needed capability appears missing, it’s worth checking the standard modules before turning to third‑party packages.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Memory OptimizationIteratorcontext managerString Manipulationresource-limitsimport-controltotal_ordering
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

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.