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

Rick Johnson rantingrickjohnson at gmail.com
Wed Mar 16 23:38:21 EDT 2016


On Wednesday, March 16, 2016 at 8:32:12 PM UTC-5, Andrew Barnert via 
Python-ideas wrote:

> This seems like something that shouldn't be done in general, so doesn't 
> need a language fix--but if you for some specific reason need to do it all 
> the time, just write a function for it: 
>
>     def _postimport(mod, names): 
>         g = sys._getframe(1).f_globals 
>         for name in names.split(): 
>             g['_'+name] = mod[name] 
>
> So: 
>
>     import thingy 
>     _postimport(thingy, 'spam eggs cheese') 
>     use(_spam, _cheese) 
>
> Of course you can wrap up the import and _postimport in a single function: 
>
>   _magic_from_import('thingy', 'spam eggs cheese') 
>
> Or use MacroPy to make the syntax nicer. 
>
> Or, if even that's not good enough, write a simple token-processing import 
> hook that finds "_import" tokens and converts them to calls to your wrapper 
> function. 
>
> Of course the farther you go down this path, the less readable your code 
> becomes to someone who doesn't know about your function/macro/hook, but if 
> there's really so much boilerplate that it's getting in the way, the 
> tradeoff might be worth it. 
>

I like your ideas, and thanks for taking the time to write up a few lines 
of code. Looks like this will have to be a feature that i implement for 
myself. 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*

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20160316/4ef69270/attachment-0001.html>


More information about the Python-ideas mailing list