Skip to content

Instantly share code, notes, and snippets.

@Tishka17
Last active July 31, 2025 09:11
Show Gist options
  • Save Tishka17/6bc58d27f5f73ac5772b2927fc9d8dfc to your computer and use it in GitHub Desktop.
Save Tishka17/6bc58d27f5f73ac5772b2927fc9d8dfc to your computer and use it in GitHub Desktop.
Python declarative modules
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
from mymod.a import CONST
from mymod.b import CONST as CONST2
print(CONST, CONST2)
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