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

13 views
Skip to first unread message

Ralf W. Grosse-Kunstleve

unread,
Feb 20, 2009, 12:52:51 AM2/20/09
to python...@python.org

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.
_______________________________________________
Python-ideas mailing list
Python...@python.org
http://mail.python.org/mailman/listinfo/python-ideas

Chris Rebert

unread,
Feb 20, 2009, 2:11:48 AM2/20/09
to Ralf W. Grosse-Kunstleve, python...@python.org
On Thu, Feb 19, 2009 at 9:52 PM, Ralf W. Grosse-Kunstleve
<rw...@yahoo.com> 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.

IMHO, that seems like it would unreasonably blur the line between
whether a module is a class or an instance. If it has a __call__
definition in it, that would seem to imply that it is somehow a class.
But since you want to be able to call it on the module itself, that
seems to suggest that the module is an instance; but in that case,
lookup would start in the class 'module', not the module itself, and
thus fail. Seems your proposal would require modules to be some
strange hybrid and an exception to the normal Python rules. On this, I
defer to the Zen: " Special cases aren't special enough to break the
rules."

I don't find the StringIO case very odd, though I do think renaming
the module (and others in similar situations) to comply with PEP8
(i.e. "stringio" ) would help.

Cheers,
Chris

--
Follow the path of the Iguana...
http://rebertia.com

Christian Heimes

unread,
Feb 20, 2009, 12:35:00 PM2/20/09
to python...@python.org
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.

Christian

Guido van Rossum

unread,
Feb 20, 2009, 12:47:28 PM2/20/09
to Christian Heimes, python...@python.org
Oh, we could easily add a __call__ to the module type that looks for a
__call__ function. I just don't think it's a good idea.

--
--Guido van Rossum (home page: http://www.python.org/~guido/)

Steven D'Aprano

unread,
Feb 20, 2009, 12:52:14 PM2/20/09
to python...@python.org
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?


--
Steven

Reply all
Reply to author
Forward
0 new messages