A fancy way of encapsulating methods. It is simple enough that I can just write a few examples and that is it.
class MyDecorator:
@staticmethod
def log(func): # This is a decorator inside a class
def wrapper(*args, **kwargs):
print(f"Calling function {func.__name__} with arguments {args}, {kwargs}")
result = func(*args, **kwargs)
print(f"Function {func.__name__} returned {result}")
return result
return wrapper
# Using @MyDecorator.log (similar to @decorator.something)
@MyDecorator.log
def add(x, y):
return x + y
print(add(3, 5))