What I want to do is something like this:
template.py
def dummy_func():
print(VAR)
# more functions like this to follow
fabfile.py
# this gets called by fabric (fabfile.org)
# safe to think of it as ant build.xml
import template
template.VAR = 'some_val'
from template import *
Namely I have a template module other modules should ‘extend’ contributing the required variables.
Can this be done in a functional manner (as opposed to object inheritance)?
EDIT: Added a bit more code.
I’m not sure what you mean by “a functional manner” — do you mean, as in functional programming? That’s not going to happen (since you’re intrinsically trying to modify an object, which is the reverse of FP). Or do you mean something like “a way that works”?
For the latter interpretation, the big problem is the
import *part — among the many problems that suggest not using that, you’re going to be running smack into one: it performs a snapshot of whatever module-level names are bound at the time (or just those listed in__all__in the module, if that’s defined) — future changes to the bindings of names will never be reflected in modules that previously did theimport *.Why do you believe you need to merge the
template_module‘s namespace into that of the importing module? If you just did a regularimport template_module as tm, then simply referring to all the relevant names astm.this,tm.thatwill work just fine (including picking up all changes to the bindings up to the instant of use — in other words, it uses the “late binding” approach that you appear to require here).