[Python-ideas] Packages and Import

Brett Cannon brett at python.org
Fri Feb 9 19:24:51 CET 2007


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?

-Brett



More information about the Python-ideas mailing list