[Python-ideas] making a module callable
Chris Angelico
rosuav at gmail.com
Wed Nov 20 00:20:51 CET 2013
On Wed, Nov 20, 2013 at 9:37 AM, Terry Reedy <tjreedy at udel.edu> wrote:
> In order to make modules callable, ModuleType must have a __call__ method.
> In order to make the call execute code in the module, that method should
> delegate to a callable in the module instance that has a known special name,
> such as __main__.
>
> class ModuleType():
> def __call__(self, *args, **kwds):
> return self.__main__(*args, **kwds)
Hmm Classes allow you to control the metaclass. Should modules allow
such a declaration? That would make this sort of thing fully
customizable.
But is there any way to avoid the chicken-and-egg problem of trying to
logically put that into the same source file as the module whose
metaclass is being changed? Considering that the creation of a class
involves building up its dictionary of contents and _then_ calling
type(), it could in theory be possible to build up a dictionary of
module contents, possibly find something with a magic name like
__metamodule__, and then use that as the module's type. But this might
become rather convoluted.
ChrisA
More information about the Python-ideas
mailing list