[Python-ideas] TypeError: 'module' object is not callable

Steven D'Aprano steve at pearwood.info
Fri Feb 20 18:52:14 CET 2009

Christian Heimes wrote:
> Ralf W. Grosse-Kunstleve wrote:
>> I see there have been discussion about module __call__ about three years ago:
>> http://mail.python.org/pipermail/python-list/2006-February/thread.html#366176
>> Is there an existing pronouncement on this subject?
>> __call__ would help avoiding strange things like from StringIO import StringIO
>> or having to come up with silly names like run, driver, manager, etc.
>> Ideally, __call__ could be either a function or class.
>> I imagine, nothing special, except that a module object looks for __call__ instead
>> of producing a type error.
> It's technically not possible without jumping through several loops.
> Magic methods are looked up on the class. Modules are instances of the
> ModuleType class.

It shouldn't be that difficult to implement. Something like:

class ModuleType:  # probably implemented in C?
     def __call__(self, *args):
         return self.__dict__['__call__'](*args)

The question is, should it be implemented?


More information about the Python-ideas mailing list