Created
May 11, 2025 14:47
-
-
Save ynkdir/56f7b046226421d9324334f44e69a158 to your computer and use it in GitHub Desktop.
shadowing definition by decorator
This file contains hidden or 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
# shadowing definition by decorator | |
import sys | |
class shadowing: | |
def __new__(cls, klass): | |
self = super().__new__(cls) | |
self._klass = klass | |
setattr(sys.modules[klass.__module__], klass.__name__, self) | |
return klass | |
def __del__(self): | |
delattr(sys.modules[self._klass.__module__], self._klass.__name__) | |
@shadowing | |
class X: | |
pass | |
print(getattr(sys.modules[__name__], "X", "There is no X")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment