[Python-ideas] Class autoload

Nick Coghlan ncoghlan at gmail.com
Sun Mar 4 06:38:34 EST 2018


On 4 March 2018 at 03:42, Chris Angelico <rosuav at gmail.com> wrote:

> On Sun, Mar 4, 2018 at 4:12 AM, Jamesie Pic <jpic at yourlabs.org> wrote:
> >
> > Hello everybody,
> >
> > I thought perhaps we could allow the usage of a "new" keyword to
> instanciate
> > an object, ie:
> >
> >    obj = new yourmodule.YourClass()
> >
> > In this case, it would behave the same as from yourmodule import
> YourClass;
> > obj = YourClass(), except that it wouldn't need to be imported. This
> would
> > also eliminate the need to manage an import list at the beginning of a
> > script in most case.
> >
>
> This actually has nothing to do with classes. You can currently write this:
>
> import yourmodule
> obj = yourmodule.YourClass()
>
> without any sort of 'new' keyword. So presumably what you're asking
> for is a way to avoid typing the 'import' statement.
>
> That's something that's come up every once in a while. Usually for the
> benefit of throwaway scripts and the interactive interpreter, because
> in serious applications, a single 'import' line is a small price to
> pay for the clarity. You may want to dig through the archives to find
> the arguments for and against this sort of automated import.
>
> > I'm really not proud of this idea but PHP has had autoload for years and
> > when i open scripts with hundred lines of imports it makes me think
> Python
> > could do something about this.
>
> A hundred lines of imports? Surely an exaggeration... or possibly you
> have a whole lot of "from module import name" lines that could become
> a single "import module" line.


Excessive numbers of top level imports in a single file are also frequently
a sign that that module has too many responsibilities and could stand to be
split up (either as multiple parallel APIs, or by splitting out a lower
level helper library) for ease of comprehension. Those split up API
implementations can then be aggregated back together into a single public
API (e.g. in a package `__init__.py` file)

As Chris has noted, function level lazy imports are also already supported,
and we've (begrudgingly) made the import system work harder to successfully
resolve circular imports in recent releases (they're also much harder to
dead lock in Python 3 than they were in Python 2, since we also switched
over to per-module import locks).

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20180304/815af774/attachment.html>


More information about the Python-ideas mailing list