[Python-Dev] New universal import mechanism ( Re: [Python-checkins] python/dist/src/Python import.c,2.210,2.211)

Jeff Epler jepler@unpythonic.net
Tue, 3 Dec 2002 10:03:09 -0600


On Tue, Dec 03, 2002 at 03:24:59PM +0100, Samuele Pedroni wrote:
> Why then only import * should have a user-defined behavior?
> 
> What about
> 
> from module import x
> 
> if the importing module has already a defined x then maybe that should be also
> under user control?
> 
> I'm surely missing something.

I don't have a good sense of why this feature is desirable, I was just
suggesting how David's request might be implemented without introducing
new __special__ names.

If it was desired that 'from module import x' be able to use the special
behavior, then I'd suggest leaving __all__ as it is now, and having
__export__, with the default behaving about like this:

    def __export__(exporter, importer, names):
        for name in names:
            setattr(importer, name, getattr(exporter, name))

and David's __export__ being about like this:

    def __export__(exporter, importer, names):
        for name in names:
            attr = getattr(exporter, name)
            if isinstance(attr, Multimethod):
                merge_one_multimethod(exporter, name, attr)
            else:
                setattr(exporter, name, attr)

maybe __export__ should be called once per name, or maybe a special
value should be passed for * (instead of __all__)

Of course, 'from m import *' has that "do with me what you will" feeling
to it, while 'from m import x' doesn't.

Jeff