
On 11/24/2013 05:11 PM, Guido van Rossum wrote:
> Why not make it so that a module function named __main__, if it exists, > gets executed when the module is run as a script?
I consider the fact that the semantics of __main__ execution are largely the same as those of any other module import to be a feature rather than a bug.
Right!
Keep in mind that we *can't* stop the current idiom from working (since we have to run the top level code to build the module in the first place), and that "run this script from disk" is just one way of executing __main__. For example, the REPL loop is a statement-by-statement interactive rendition of __main__, while __main__.py files in zipfiles, directories and packages don't bother with the "if __name__ == '__main__'" guard at all.
Right, it prevents that section from running in the case where it's not the main module. But spelling that explicitly isn't as nice. if not __name__ != "__main__": # Don't do this if this module isn't named "__main__". Regarding the is_main()... seems like it should take an argument. Most is_something() functions do. Is there a way to make something like the following work? def is_main(name=None): if name == None: name = get_attr_from_caller() # dynamic lookup return (name == "__main__") And how about just a global name __main__ that is always set to "__main__"? if __name__ is __main__: ... Not a big change, but it reads nice and maybe users will like it well enough not to keep suggesting changing it. ;-) Down the road (python6) you could change both of those to the real main modules name and they would still work. Just a few thoughts, Ron