module with __call__ defined is not callable?

Fuzzyman fuzzyman at gmail.com
Wed Feb 8 07:18:33 EST 2006


Steve Holden wrote:
> Fuzzyman wrote:
> > Steven D'Aprano wrote:
> >
> >>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.
> >>
> >
> >
> > It would nice if you could make modules callable.
> >
> Right. While we're at it, why don't we make strings callable. Calling a
> string could call the function whose name (in some namespace or other)
> was in the string. And we can make integers callable too - that could
> just assume that the integer was the address of a function to be called.
>
> In case you think I'm joking, I am.
>
> Why should a module be callable? What's the advantage? Should we be able
> to add two modules together, yielding a module that contains all the
> code of both modules? What happens if I multiply a module by two -
> presumably the result should be the same as adding a module to itself?
> Perhaps we should be able to divide a module by a function?
>
> The pursuit of orthogonality, while admirable, can lead to insanity if
> pushed too far.
>

Sure - feel free to venture as far down the road of insanity as you
like :-) To pursue your analogy, why don't we answer all usenet posts
by pushing suggestions to ridiculous levels ? ;-)

What would actually be the problem with allowing modules to define a
__call__ though ? This would allow for nice clean namespaces at the
module level.

Ok, so there are other ways of doing it - but it's not at all
illogical.

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml

> regards
>   Steve
> --
> Steve Holden       +44 150 684 7255  +1 800 494 3119
> Holden Web LLC                     www.holdenweb.com
> PyCon TX 2006                  www.python.org/pycon/




More information about the Python-list mailing list