On Sep 24, 2014, at 11:10, "M.-A. Lemburg"
On 24.09.2014 19:57, Thomas Gläßle wrote:
Hey folks,
What do you think about making it easier to use packages by automatically importing submodules on attribute access.
Consider this example:
import matplotlib figure = matplotlib.figure.Figure() AttributeError: 'module' object has no attribute 'figure'
For the newcomer (like me some months ago) it's not obvious that the solution is to import matplotlib.figure.
Worse even: it may sometimes/later on work, if the submodule has been imported from another place.
How, I'd like it to behave instead (in pseudo code, since `package` is not a python class right now):
class package:
def __getattr__(self, name): try: return self.__dict__[name] except KeyError: # either try to import `name` or raise a nicer error message
The automatic import feature could also play nicely when porting a package with submodules to or from a simple module with namespaces (as suggested in [1]), making this transition seemless to any user.
I'm not sure about potential problems from auto-importing. I currently see the following issues:
- harmless looking attribute access can lead to significant code execution including side effects. On the other hand, that could always be the case.
- you can't use attribute access anymore to test whether a submodule is imported (must use sys.modules instead, I guess)
In principle one can already make this feature happen today, by replacing the object in sys.modules - which is kind of ugly and has probably more flaws. This would also be made easier if there were a module.__getattr__ ([2]) or "metaclass" like feature for modules (which would be just a class then, I guess).
Sorry, if this has come up before and I missed it. Anyhow, just interested if anyone else considers this a nice feature.
Agreed, it's a nice feature :-)
I've been using this in our mx packages since 1999 using a module called LazyModule.py. See e.g. http://educommons.com/dev/browser/3.2/installers/windows/src/eduCommons/pyth...
Could LazyModule be easily added to the stdlib, or split out into a separate PyPI package? It seems to me that would be a pretty good solution. Today, a package has to eagerly preload modules, make the users do it manually, or write a few dozen lines of code to lazily load modules on demand, so it's not surprising that many of them don't use the third option even when it would be best for their users. If that could be one or two lines instead, I'm guessing a lot more packages would do so.