On 01/04/2011 04:52 PM, Guido van Rossum wrote:
Hmm... I starred this and am finally dug out enough to comment.
Would it be sufficient if the __module__ attribute of classes and functions got set to the "canonical" name rather than the "physical" name?
You can currently get a crude version of this by simply assigning to __name__ at the top of the module.
That sounds like it would be too confusing, however, so perhaps we could make it so that, when the __module__ attribute is initialized, it first looks for __canonical__ and then for __name__?
This may still be too crude though -- I looked at the one example I could think of where this might be useful, the unittest package, and realized that it would set __module__ to 'unittest' even for classes that are not actually re-exported via the unittest namespace.
So maybe it would be better in that case to just patch the __module__ attribute of all the public classes in unittest/__import__.py?
OTOH for things named __main__, setting __canonical__ (automatically, by -m or whatever other mechanism starts execution, like "python
<filename>" might actually work.
On the third hand, maybe you've finally hit upon a reason why the "if __name__ == '__main__': main()" idiom is bad...
(This is probably something that was suggested more than a few times before.)
Would it help if global name space acquired a __main__ name? Then the standard if line becomes only a slightly different "if __name__ == __main__: main()". I think that would make more sense to beginners also and it is a bit less magical.
For now, both ways could work, __main__ would be "__main__" or None, but down the road, (long enough to be sure everyone knows to drop the quotes), both __main__ and __name__ could be switched to the actual module name so that __name__ and __module__ attributes would always be correct.