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!