
On Mar 16, 2016, at 18:13, Rick Johnson <rantingrickjohnson@gmail.com> wrote:
On Wednesday, March 16, 2016 at 6:39:01 PM UTC-5, Ben Finney wrote:
Agreed. What is wrong with:
import lorem as _lorem do_something_with(_lorem.Foo)
Yes, in the case when multiple symbols are imported+privatized, that's an improvement, and hiding multiple symbols behind a single symbol *CAN* help to avoid name clashes as well (less names, less chances of clashing) but, in essence, all you've done is to move "syntactic noise" from a single location, to a minimum of one other location, and, potentially, many diverse locations. So yes, useful technique, but not one that fulfills my current "selfish desires" -- I need something sweet baby! O:-)
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.