[Python-ideas] Importing public symbols and simultainiously privatizing them, is too noisy

Eric Snow ericsnowcurrently at gmail.com
Thu Mar 17 13:25:09 EDT 2016


On Mar 16, 2016 21:38, "Rick Johnson" <rantingrickjohnson at 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160317/3fe98e88/attachment.html>


More information about the Python-ideas mailing list