module not callable - why not?

John Roth newsgroups at jhrothjr.com
Fri Apr 9 14:42:15 EDT 2004


"djw" <dwelch91 at comcast.net> wrote in message
news:VKAdc.360$rC3.22993 at attbi_s53...
> Diez B. Roggisch wrote:
> > Hi,
> >
> > I just thought about creating a module for quaternions (as an personal
> > exercise, so I'm not after pointers to classlibs here).
> >
> > Now usually I'd create a file called "quaternion.py", define my
quaternion
> > class in there and then import and create an instance like this:
> >
> > import quaternion
> >
> > q = quaternion.quaternion()
> >
> > Thats a lot to type. doing a
> >
> > from quaternion import quaternion
> >
> > would solve that - but AFAIK thats considered bad for some reasons.
>
> I think what people consider dangerous is 'from <module> import *'. The
> form you give here is OK, as far as I know.
>
> >
> > Now I thought about defining a __call__ operator at module level to
allow
> > this:
> >
> > import quaternion
> >
> > q = quaternion()
> >
> >
> > where the call looks like this:
> >
> > def __call__(*a, *kw):
> >     return quaternion()
> >
> > But that doesn't work.
> >
> > Now my question is: Is there a way to make a module callable that way?
And
> > wouldn't it make sense to allow the implementation of a call operator on
> > module level?
>
> I know this has been discussed before (using __main__() instead). Try
> googling for some discussions.

The discussion on __main__() isn't quite the same thing. If I remember
correctly, the use case for __main__ was to get rid of the two line suffix
to a script to make it executable. This is rather obscure to novices, while
__main__() would be a bit more understandable. At least, it's similar
to what other languages do for a startup function. There was no intention
that __main__() be called on an import, or that it be accessable via the
normal call syntax on the module object. At least, I don't remember such.

John Roth
>
> -Don
>





More information about the Python-list mailing list