[Python-ideas] Class autoload

Steven D'Aprano steve at pearwood.info
Sat Mar 3 19:25:13 EST 2018


On Sat, Mar 03, 2018 at 06:12:06PM +0100, Jamesie Pic 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. 

Of course it would still need to be imported, it just wouldn't be 
imported *once* into the current module. So:

a = new yourmodule.YourClass()
b = new yourmodule.YourClass()
c = new yourmodule.YourClass()
d = new yourmodule.YourClass()

would have to go through the process of importing yourmodule *four* 
times. Admittedly only the first time would be really expensive (and you 
can't escape that: this applies to `import` too), but the others won't 
be free.

It will also break pickling. (I think.)

There's also the serious cost of adding a new keyword, breaking 
everyone's code that uses "new" for something else. There has to be a 
good reason to do that, better than just "PHP offers this" or "I don't 
like managing imports".


> 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.

Somehow I don't feel that "hundreds of lines of imports" in a single 
script is realistic. Do you have an example of a publicly viewable 
script with hundreds of lines of imports?


-- 
Steve


More information about the Python-ideas mailing list