[Python-ideas] Class autoload

Chris Angelico rosuav at gmail.com
Sat Mar 3 12:42:14 EST 2018


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. Also, "PHP does this" is a terrible
justification for a feature... :)

ChrisA


More information about the Python-ideas mailing list