On 11/24/2013 2:10 PM, Guido van Rossum wrote:
Haven't followed all of this, but perhaps the simplest thing would be to define a new builtin function that returns True in the namespace of the main module and false everywhere else. It could be implemented by pulling '__name__' out of the caller's local namespace and comparing it to '__main__'. We could name this function __main__(), or perhaps less dramatic, is_main(). Then you could write
if is_main(): <do your main code>
Writing 'is_main' (or anything else) instead of '__name__=='__main__' seems silly if it would be the only thing breaking back compatibility.
For people who want to use this idiom on older platforms too they can easily implement it themselves using sys._getframe.
...if one know about sys._getframe and how to use it. The leading underscore implies that it is subject to change in each version. Writing an import (from somewhere dependable) for such a function will be as many keystrokes as writing the idiom. For my main project, I have a template file that includes 'if __name__.. <run test>' along with other project boilerplate. -- Terry Jan Reedy