Python Daniel with the thief slip, 9 practical skills to share with you


This article will try to introduce some tips that are not mentioned in other articles, which I usually use. Let’s find out!

Python Daniel with the thief slip, 9 practical skills to share with you

Sorting string input

The problem of sorting user input is very common in the programming process. Usually, converting characters to lowercase or uppercase is enough. Sometimes you can use the regex module to do this. But if the problem is complex, there may be a better way to solve it:

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

In this case, you can see that the space characters “n” and “t” are replaced with single spaces, and “R” is deleted. This is just a very simple example. We can go a step further by using the “Unicode data” package to generate a large remapping table, and using the “combining ()” in it to generate and map. We can

Iterator slice

If the iterator is sliced, a “typeerror” will be returned, indicating that the generator object has no subscript. However, we can use a simple solution to solve this problem

import itertools  
s = itertools.islice(range(50), 10, 20)  # <itertools.islice object at 0x7f70fab88138>  
for val in s:  

We can use “itertools. Islice” to create an “islice” object, which is an iterator and can generate the items we want. However, it should be noted that this operation uses all generator items before slicing and all items in the “islice” object.

Skip the beginning of an iteratable object

Sometimes you have to deal with files that start with unnecessary lines, such as comments “Itertools” once again provides a simple solution:

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

This code prints only after the initial comment section. This is useful if we just want to get rid of the beginning of the iteratable object (in this case, the comment line at the beginning) and don’t know how long it will be.

Functions with keyword arguments only (kwargs)

When we use the following functions, it is helpful to create functions that only need keyword parameters as input to provide clearer function definitions:

def test(*, a, b):  
test("value for a", "value for b")  # TypeError: test() takes 0 positional arguments...  
test(a="value", b="value 2")  # Works...

As you can see, add a “U” before the keyword parameter We can solve this problem. If we put some parameters in the Before parameters, they are obviously positional parameters.

Create an object that supports the with statement

For example, we all know how to use the “with” statement to open a file or get a lock, but can we implement our own context expression? Yes, we can use “enter“And”exitTo implement the context management protocol:

class Connection:  
    def __init__(self):  
    def __enter__(self):  
        # Initialize connection...  
    def __exit__(self, type, value, traceback):  
        # Close connection...  
with Connection() as c:  
    # __enter__() executes  
    # conn.__exit__() executes

This is the most common way to implement context management in Python, but there are simpler ways:

from contextlib import contextmanager  
def tag(name):  
with tag("h1"):  
    print("This is Title.")

The above code implements the content management protocol using the manager decorator of contextmanager. When you enter the with block, the first part of the tag function (before yield) is already executed, then the with block is executed, and finally the rest of the tag function is executed.

Use “slotsSave memory

If you’ve ever written a program that creates a large number of instances of a certain kind, you may have noticed that your program suddenly needs a lot of memory. That’s because Python uses dictionaries to represent the properties of class instances, which makes it very fast, but memory usage is not very efficient. Usually, this is not a serious problem. However, if your program is seriously affected by this, you might as well tryslots」:

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  = phone

When we defineslotsPython uses a small fixed size array instead of a dictionary, which greatly reduces the memory required for each instance. Use “slots“Has some disadvantages: we can’t declare any new properties, we can only use”slotsThe existing properties on the. In addition, with theslots‘cannot use multiple inheritance.

Limit CPU and memory usage

If you don’t want to optimize the memory or CPU utilization of the program, but want to limit it to a certain number directly, python also has a corresponding library to do this:

import signal  
import resource  
import os  
# To Limit CPU time  
def time_exceeded(signo, frame):  
    print("CPU exceeded...")  
    raise SystemExit(1)  
def set_max_runtime(seconds):  
    # Install the signal handler and set a resource limit  
    soft, hard = resource.getrlimit(resource.RLIMIT_CPU)  
    resource.setrlimit(resource.RLIMIT_CPU, (seconds, hard))  
    signal.signal(signal.SIGXCPU, time_exceeded)  
# To limit memory usage  
def set_max_memory(size):  
    soft, hard = resource.getrlimit(resource.RLIMIT_AS)  
    resource.setrlimit(resource.RLIMIT_AS, (size, hard))

We can see that in the above code fragment, there are options to set the maximum CPU running time and the maximum memory usage limit. When limiting the CPU’s running time, we first obtain the specific resource (rlimit)_ CPU), and then use the seconds specified by the parameter and the previously retrieved hard limit to set. Finally, if the CPU’s running time exceeds the limit, we will send out the system exit signal. In terms of memory usage, we retrieve the soft limit and hard limit again, and use the “setrlimit” with the “size” parameter and the previously retrieved hard limit to set it.

Controls what can / cannot be imported

Some languages have a very obvious mechanism to export members (variables, methods, interfaces). For example, in golang, only members beginning with capital letters are exported. However, in Python, all members are exported (unless we use “all」):

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

In the above code, we know that only the “bar” function is exported. Similarly, we can make theallThis will not export anything. When importing from this module, “attributeerror” will be caused.

A simple way to implement comparison operator

Implement all comparison operators for a class (such as  lt , le , gt , ge)It’s very complicated. Is there an easier way to do this? In this case, “functools. Total”_ “Ordering” is a good helper

from functools import 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))

How does this work? We use “total”_ The “ordering” decorator simplifies the process of sorting class instances. We just need to define “lt“And”eqThey are the smallest set of operations needed to implement the rest of the operations.


Not all of the features mentioned in this article are necessary or useful in everyday python programming, but some of them may come in handy from time to time, and they may simplify some tedious and troublesome tasks. It should also be noted that all of these features are part of the python standard library. In my opinion, some of the functions are not like the standard content contained in the standard library, so when you use Python to implement some of the functions mentioned in this article, please refer to the python standard library first. If you can’t find the function you want, it may be because you haven’t tried your best to find it (if not, it must also exist in some third-party libraries).

At the end of the article, we summarize the most complete Python data in history (long-term update). The children next door are crying

Python Daniel with the thief slip, 9 practical skills to share with you