[Python-ideas] making a module callable

Haoyi Li haoyi.sg at gmail.com
Wed Nov 20 22:14:05 CET 2013


> Sticking something into sys.modules to replace the currently executing module
is indeed a hack.  The import system accommodates this not by design
(unless someone is willing to come forward and admit guilt <wink>) but
mostly as an incidental implementation artifact of the import machinery
from many releases ago.

Yeah, and the fact that people are jumping throw these hoops and doing
"nasty hacks" despite their nastiness means that there's a real need for
the functionality. If it was easy and people did it, then we don't learn
anything, same if it's difficult and people don't do it.

On the other hand, if a feature is easy and people don't do it, then maybe
that feature deserves to be deprecated/made less easy. Similarly, if it's
difficult/nasty/hacky and you find people doing it anyway, then the
functionality probably deserves to be made easier to use. The hackiness if
an artifact of the way things are now, but this whole thread is about
changing the way things are now. We should be shaping the machinery to fit
what people do, rather than trying to shape people to fit the machinery
which was arbitrarily designed a long time ago.


On Wed, Nov 20, 2013 at 12:23 PM, Ethan Furman <ethan at stoneleaf.us> wrote:

> On 11/20/2013 12:14 PM, Eric Snow wrote:
>
>>
>> Sticking something into sys.modules to replace the currently executing
>> module is indeed a hack.  The import system accommodates this not by
>> design (unless someone is willing to come forward and admit guilt
>> <wink>) but mostly as an incidental implementation artifact of the
>> import machinery from many releases ago. [1]
>>
>
> Actually, it is intentional.  An excerpt from https://mail.python.org/
> pipermail/python-ideas/2012-May/014969.html
>
>   There is actually a hack that is occasionally used and recommended:
>>  a module can define a class with the desired functionality, and then
>>  at the end, replace itself in sys.modules with an instance of that
>>  class (or with the class, if you insist, but that's generally less
>>  useful). E.g.:
>>
>> # module foo.py
>>
>>   import sys
>>
>>   class Foo:
>>       def funct1(self, <args>): <code>
>>       def funct2(self, <args>): <code>
>>
>>   sys.modules[__name__] = Foo()
>>
>> This works because the import machinery is actively enabling this
>>  hack, and as its final step pulls the actual module out of
>>  sys.modules, after loading it. (This is no accident. The hack was
>>  proposed long ago and we decided we liked enough to support it in
>>  the import machinery.)
>>
>
> --
> ~Ethan~
>
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20131120/204195f3/attachment-0001.html>


More information about the Python-ideas mailing list