Callable modules?

Alex Martelli aleax at aleax.it
Wed Jul 24 07:54:27 EDT 2002


Paul Rubin wrote:

> martin at v.loewis.de (Martin v. Loewis) writes:
>> You can put a callable object in sys.modules. Module objects
>> themselves are not callable.
> 
> Is this documented somewhere?  I don't see it in the obvious places.

I could claim it's documented in the cookbook (both online and
in print), but I don't think I remembered to list __call__ among
the special methods you can use this way -- my specific recipe
deals with __setattr__ and __delattr__, and I did also mention
__getattr__ in the discussion text, but of course the same idea
generalizes to other specials.


> If the main purpose of the module is to provide one function, I think
> it's cleaner to be able to import the module and call the function
> without special tricks.

A respectable opinion.  Mine is just the reverse -- were it not
for backwards compatibility, I wouldn't mind losing __call__ in
instances.  A bound method can do it, and we could have a
_convention_ about how a "primary" method (if any) should be
named, just as we have for the 'self' argument.  It's all
theoretical anyway, __call__'s never going to go away in fact.

You may be right that, given that many types have __call__, so
should the module type (there being no accepted convention for
the name of the 'primary' method).  But then why not, e.g.,
__str__, or __iter__?  Where (if anywhere) do we draw the line?


Alex




More information about the Python-list mailing list