module with __call__ defined is not callable?

Steven D'Aprano steve at REMOVETHIScyber.com.au
Wed Feb 8 05:21:25 EST 2006


On Wed, 08 Feb 2006 13:58:13 +1100, Delaney, Timothy (Tim) wrote:

> adam johnson wrote:
> 
>> Hi All.
>> I was wondering why defining a __call__ attribute for a module
>> doesn't make it actually callable. 
> 
> For the same reason that the following doesn't work
[snip example]
> The __call__ attribute must be defined on the class (or type) - not on
> the instance. A module is an instance of <type 'module'>.

That's not a _reason_, it is just a (re-)statement of fact. We know that
defining a __call__ method on a module doesn't make it callable. Why not?
The answer isn't "because defining a __call__ method on a module or an
instance doesn't make it callable", that's just avoiding the question.

Someone had to code Python so that it raised an error when you try to call
a module object. Is there a reason why module() should not execute
module.__call__()? I would have thought that by the duck typing principle,
it shouldn't matter whether the object was a class, a module or an int, if
it has a __call__ method it should be callable.


-- 
Steven.




More information about the Python-list mailing list