[Python-ideas] Just __main__

Andrew McNabb amcnabb at mcnabbs.org
Mon Jun 18 20:05:26 CEST 2012


On Mon, Jun 18, 2012 at 01:25:11PM -0400, Mike Graham wrote:
> 
> That being said, I'm -0 on the feature. I don't think it's really much
> easier to explain or worth any effort.

I agree that having a boolean called "__main__" wouldn't add much value,
but I believe that recognizing a function called "__main__" could
potentially add a bit more value.

After executing the body of a script, the interpreter would
automatically call the "__main__" function if it exists, and exit with
its return value.  Thus:

def __main__():
    return 42

would be roughly equivalent to:

if __name__ == '__main__':
    sys.exit(42)

It might make sense to have "python -i" not call the "__main__"
function, making it easier to interact with a script after the time that
its methods and global variables are all defined but before the time
that it enters __main__.

I'm not sure if a "__main__" function would add enough value, but I
think it would add more value than a "__main__" boolean.

--
Andrew McNabb
http://www.mcnabbs.org/andrew/
PGP Fingerprint: 8A17 B57C 6879 1863 DE55  8012 AB4D 6098 8826 6868



More information about the Python-ideas mailing list