Last active
July 31, 2025 09:11
-
-
Save Tishka17/6bc58d27f5f73ac5772b2927fc9d8dfc to your computer and use it in GitHub Desktop.
Python declarative modules
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
import sys | |
import types | |
def module(cls): | |
class tmp(cls, types.ModuleType): | |
pass | |
tmp.__name__ = cls.__name__ | |
instance = tmp(f"{cls.__module__}.{cls.__name__}") | |
sys.modules[instance.__name__] = instance | |
return instance |
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
from mymod.a import CONST | |
from mymod.b import CONST as CONST2 | |
print(CONST, CONST2) |
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
from declmod import module | |
@module | |
class a: | |
CONST = 1 | |
@module | |
class b: | |
CONST = 2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment