On Mar 16, 2016 21:38, "Rick Johnson" <rantingrickjohnson@gmail.com> wrote:
> Heck, I had already planned on re-implementing the entire import mechanism anyway, because I like to spread my module source code across multiple files, and i want to explicitly define *HOW* those source files are combined to create namespaces at run-time, so that i can keep them separate, whist easily sharing state between them.. Python does not allow me to do this without resorting to "import contortions" and monkey patching -- but i know how to do it! *evil grin*

So like Go does packages?  You should be able to do that using a custom loader and a path-entry finder to provide that loader.  See the importlib docs for more info.  Just be careful to only handle your special directories or else normal packages will break.

FWIW, there are more idiomatic ways to combine multiple files into a single module.  For instance, make a package containing your separate files but make the files private (leading underscore).  Then put "from XXX import *" for each of them.  If you want to limit what's exported from the package, either define __all__ in the sub-files or use "from XXX import YYY" in the package __init__.py.

As far as sharing a namespace between the sub-files, I'd recommend against an implicit mechanism (like Go does).  I've found that it makes it harder to discover code.

-eric