On 1/4/2011 5:52 PM, Guido van Rossum wrote:
Nick's concern does not affect me,
On the third hand, maybe you've finally hit upon a reason why the "if __name__ == '__main__': main()" idiom is bad...
but I use this all the time. A suggested alternative and possible eventual replacement: give every module an attribute __main__ set to either True or False. Then the idiom would be much simpler and easier to learn and write: 'if __main__: ...'.
If there were no other use of the fake '__main__' name, the simple and unconditional replacement would be much less disruptive than, say, the int division change. But the first 10 pages of codesearch on '__main__' shows things like
django/test/_doctest.py - 107 identical elif module.__name__ == '__main__':
1850: m = sys.modules.get('__main__')
another sys.modules.get(), a sys.modules(), and
Formulator/tests/framework.py - many identical
57: if p0 and __name__ == '__main__': 58: os.chdir(p0)
The variant conditionals are easy to patch (by hand). The sys.modules lookup suggests that the main module should continue to be keyed under '__main__', even if also keyed under its 'real' name.
[Keying modules under a canonical name would eliminate duplicate import bugs, but that is another issue.]
-- Terry Jan Reedy