Skip to content

Instantly share code, notes, and snippets.

@lukacat10
Created April 29, 2024 00:13
Show Gist options
  • Save lukacat10/3779851adf8ac3b4968222c7faaff397 to your computer and use it in GitHub Desktop.
Save lukacat10/3779851adf8ac3b4968222c7faaff397 to your computer and use it in GitHub Desktop.
A class for wrapping other classes. Wasn't tested :)
from typing import TypeVar, Generic
T = TypeVar("T") # Declare a type variable
class Wrapper(Generic[T]):
def __init__(self, wrapped_obj: T):
self._wrapped_obj = wrapped_obj
def __getattr__(self, attr):
# Delegate attribute access to the wrapped object
return getattr(self._wrapped_obj, attr)
def __setattr__(self, attr, value):
# Delegate attribute assignment to the wrapped object
if attr == "_wrapped_obj":
# If setting the wrapped object itself, call superclass method to avoid infinite recursion
super().__setattr__(attr, value)
else:
# Delegate attribute assignment to the wrapped object
setattr(self._wrapped_obj, attr, value)
def __delattr__(self, attr):
# Delegate attribute deletion to the wrapped object
delattr(self._wrapped_obj, attr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment