Sobes.tech
Middle

Are there any patterns that you directly use in your work?

sobes.tech AI

Answer from AI

In Python development, I often use the following patterns:

  • Singleton — for objects that should have a single instance, such as application configuration.

  • Factory — for creating objects with different initialization logic, simplifying extensibility.

  • Decorator — for dynamically adding functionality to functions or classes without changing their code.

  • Context Manager (with) — for resource management (files, connections), ensuring proper release.

  • Observer — for implementing event-driven models where objects subscribe to state changes.

Example of using a decorator for logging function calls:

def log_calls(func):
    def wrapper(*args, **kwargs):
        print(f"Calling {func.__name__} with args={args}, kwargs={kwargs}")
        return func(*args, **kwargs)
    return wrapper

@log_calls
def add(a, b):
    return a + b

add(2, 3)  # Will print the call log and return 5