Created
April 29, 2024 00:13
-
-
Save lukacat10/3779851adf8ac3b4968222c7faaff397 to your computer and use it in GitHub Desktop.
A class for wrapping other classes. Wasn't tested :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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