views:

777

answers:

2

In the past I've used perl's AUTOLOAD facility for implementing lazy loading of symbols into a namespace, and wanted the same functionality in python.

Traditionally the closest you appear to be able to get is to use a class and a __getattr__ class to achieve this sort of thing. However I've also tried rummaging around in sys.modules, and come up with this:

# mymod.py
def greet(greeting="Hello World"):
   print greeting

class autoload(object):
    def __init__(self, __name__):
        super(autoload, self).__init__()
        self.wrapped_name = __name__
        self.wrapped = sys.modules[__name__]
    def __getattr__(self, name):
        try:
            return getattr(self.wrapped, name)
        except AttributeError:
            def f():
                greet(name+" "+self.wrapped_name)
            return f

if __name__ != "__main__":
    import sys
    sys.modules[__name__] = autoload(__name__)

This does work the way I'd like from a user perspective:

~> python
Python 2.5.1 (r251:54863, Jan 10 2008, 18:01:57)
[GCC 4.2.1 (SUSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mymod
>>> mymod.hello()
hello
>>> from mymod import Hello_World
>>> Hello_World()
Hello_World

But it strikes me - is there a standard approach that people tend to use for autoloading in python?

Secondly, a question for experienced python developers really is "does this strike you as good or bad practice"? I'm a reasonably experienced python developer, and it strikes me as really useful, but it strikes me as borderline and interested in whether this can be viewed as good practice, bad practice or similar.

Thanks!

+7  A: 

"Lazy imports" could be built on top of the "new import hooks" specified in PEP 302, and now fully implemented. PEP 369 used to cover "lazy imports" as well as post-import hooks, but has since been simplified and now only covers post-import hooks; still, you might be interested in the original draft.

A good implementation of "lazy imports" via the meta_path hook can be found in this recipe.

Alex Martelli
A: 

No, I don't think that's useful.

Why create a function on the fly for every attribute you're trying to get from the module? Seems confusing to me. It appears that new functions are being created by magic so one has to look deep into the implementation to understand what is going on. And all that for no syntactical benefits.

And even if you had a good reason to do that, why do it with a module? Why register your class instance in sys.modules? I think making things appear what they aren't is frowned upon in python.

Unless you are obfuscating code on purpose, I don't see why one would do all that.

nosklo
this is certainly useful in some cases, when you want to minimize import time for example. But that's always borderline, and it makes things more complicated (in particular debugging). Import hooks are difficult to get right for every situation. I would avoid them as much as possible fir 'core' libraries which are intended to be used outside their primary usage.
David Cournapeau
+1 for David's comment. Lazy imports add an additional level of complexity to any module's state. It's true that it might save a few moments writing import statements but that time will be lost in debugging. Incidentally many modern IDEs have the ability to manage imports. Check out pydev on Eclipse.
Salim Fadhley