@decorator
import module
@decorator
from abc import d
In my understanding, modules (or python.py files) are like static classes in C# Where you can define functions variables, classes and so on, and you cant create instances from it.
When ever I want to use a static class, i would do
class StaticClass:
propA = "value"
propB = "value"
...
@staticmethod
def funA(cls): pass
@staticmethod
def funB(cls): pass
but i find it unhandy to write all this “@staticmethod” and having to reference cls as parameter
so i move it outside the class to the module
propA = "value"
propB = "value"
def funA():
pass
def funB():
global propA, probB
pass
the only draw bag seems to be when editing variables of the module i need to use the global keyword.
in c# you can create Template Classes, and all this idea is about to being able to create kindof module Templates / in my understanding templated static classes with out the boilerplate @staticmethod def a(cls): … stuff
Lets say i have a module with many functions which i want to decorate with the same decorator.
Those decorators take a parameter v. We could mark the variable v as Template variable by defining a new Type
class Template(Any): pass
# module.py
v:Template = "somevalue"
@x(v)
def a():...
@x(v)
def b():...
...
Now i want to import the module multiple times but i want the parameter v to be another each time.
We could define a wrapper function that takes a module as argument and modify the module and return it.
def module_decorator(globals,vars,...): #the parameters are what ever properties a module can have
or we create a new Type for Modules where those parameters are stored
def module_decorator(module:Module,otherparams,*,v):
copied_module = module.copy()
#modify module ...
copied_module.vars["v"].set(v) # set the v parameter of module.py
copied_module.vars.filterByType(Template)["v"].set(v) # set the v parameter of module.py
# we could also decorate all functions with some wrapper function at this point
so i dont have to decorate all the functions seperately with @x(v)
# copied_module.functions
# copied_module.classes
# copied_module.imported_modules
return copied_module
So now we could easly do
@module_decorator(v="A")
import module as versionA
@module_decorator(v="B")
import module as versionB
...
In my usecase i could use this to create a module which implements the same sql queries but on different sql tables, and i pass the table name to the template module.
the only draw bag would be that when looking at the module it self, you cant know if its used as template.
this could lead to confusion, but i believe it could be benifitial anyways.
Self criticism:
I might not be the first one having this idea, and i may have misunderstood the concept of modules. Maby its bad practice to use a module as c# static class and wanting to template them is not a pythonic thing.
Anyways, since i didnt find anything about this topic so far id like to start a discussion about this and maby learn from it.