functools

functools provides a range of features that can greatly simplify your code and make it more efficient. The functools module is part of the Python standard library and provides higher-order functions, decorators, and other utilities for working with functions. functools.partial: Partial Function Application Partial function application is a technique where you create a new function by fixing certain arguments of an existing function, allowing you to provide only the remaining arguments when calling the new function....

December 5, 2023 · aaron

Singleton with Decorator

In Python, a singleton is a design pattern that restricts the instantiation of a class to a single object. A singleton class ensures that only one instance of the class is created and provides a global point of access to that instance. One way to implement a singleton in Python is to use a decorator. Here’s an example: def singleton(cls): instances = {} def get_instance(*args, **kwargs): if cls not in instances: instances[cls] = cls(*args, **kwargs) return instances[cls] return get_instance @singleton class MyClass: def __init__(self, x): self....

April 4, 2023 · aaron