[Python-ideas] Packages and Import

Brett Cannon brett at python.org
Fri Feb 9 22:14:07 CET 2007


On 2/9/07, Josiah Carlson <jcarlson at uci.edu> wrote:
>
> "Brett Cannon" <brett at python.org> wrote:
> >
> > On 2/9/07, Ron Adam <rrr at ronadam.com> wrote:
> > > Brett Cannon wrote:
> > > > On 2/8/07, Ron Adam <rrr at ronadam.com> wrote:
> >
> > [SNIP]
> >
> > > >> If you remove the "__main__" name, then you will still need to have some
> > > >> attribute for python to determine the same thing.
> > > >
> > > > Why?  There is nothing saying we can't follow most other languages and
> > > > just have a reserved function name that gets executed if the module is
> > > > executed.
> > >
> > > Yes, but this is where python is different from other languages.  In a way,
> > > python's main *is* the whole module from the top to bottom.  And so the
> > > '__main__' name is referring to the whole module and not just a function in it.
> > >
> > > A more specific function would be needed to get the context right.  Maybe
> > > __script__(),  or __run__().
> > >
> > >
> > > Or if you want to be consistent with class's,  how about adding __call__() to
> > > modules?   Then the main body of the module effectively works the same way as it
> > > does in a class.  =)
> > >
> > >
> > > Hey, I think that has some cool possibilities, it makes modules callable in
> > > general.  So if I want to run a module's __call__(), AKA main() as you call it,
> > > after importing I would just do...
> > >
> > >     import module
> > >     module()
> > >
> > > And it would just work.  ;-)
> > >
> >
> > I like this idea.  Makes it very obvious.  You just say "when a
> > specific module is specified at the command line it is called.  Could
> > even have it take possibly sys.argv[1:] (which I think was supposed to
> > turn into sys.args or sys.arg or something at some point).
> >
> > What do other people think?
>
> I don't like it.  Much of my dislike comes from personal aesthetics, but
> then there is a logical disconnect.  When an instance of a class is
> created, its __call__ method is not automatically called. By using the
> semantic of 'the __call__ function in the module namespace is
> automatically executed if the module is "run" from the command line', we
> are introducing a different instance creation semantic (an imported
> module is an instance of ModuleType).
>

But I don't see the leap of how specifying a module to execute on the
command line is any different than doing ``Class()()`` for
instantiation with an immediate call.  It would still be a separate
step.

> I think we should just stick with what has been proposed for *years*, a
> __main__ function that is automatically executed after the module has
> been imported if its __name__ == '__main__'.

But that does not solve the problem Ron has been trying to deal with;
setting __name__ to __main__ prevents the execution of a module that
uses relative imports because the import machinery can then no longer
infer what package the module is in.

-Brett



More information about the Python-ideas mailing list