[Python-ideas] Class autoload

Cameron Simpson cs at cskk.id.au
Sat Mar 3 19:41:53 EST 2018


On 03Mar2018 19:46, Jamesie Pic <jpic at yourlabs.org> wrote:
>While i understand it would be harder to make it memory efficient, but this
>is python not go, and also this sort feature could be easily optional,
>also, it might even help against circular import issues, whoever hasn't
>imported a module from inside a function in their life may throw the first
>rock at my face (kidding)
>
>Yes i know it's sad php has this feature and python does not and again i'm
>not proud to say this but it's true.

In a recent incarnation I spent a lot of time writing PHP. I've nothing good to 
say about it. However, to the "feature" point above:

Its import facility is broken to the point iof being actively misleading. We 
weren't doing autoimports because it is good to be able to look at the top of a 
module and see what it uses. But even PHP's "use" statement is broken. It 
doesn't do anything until you access a name from it. _Then_ it goes to see if 
such a module exists. I blew a whole morning on this misbehaviour that didn't 
show unless a particular code path got to run.

My personal belief is that the PHP people thought "this stuff runs on the fly 
per web page load, it needs to be fast fast fast!" And therefore their "use" 
statement just loads a string mapping of where to look for a name. The looking 
doesn't happen until the name gets used, if it gets used. So you can "use" a 
totally misspelt module and not realise it until very late in the game.

We had a particularly skilled PHP guy on the team, and he and I still blew a 
couple of hours trying to figure out why one of my "use" statements didn't 
work, and that one _was_ due to some kind of autoimport magic I now forget.

Personally, I'm -1 for an autoimport because it doesn't actually buy much, and 
-2 for any autoimport based on PHP semantics, which generally lead to "fail 
late" instead of "fail early".

Cheers,
Cameron Simpson <cs at cskk.id.au>


More information about the Python-ideas mailing list