[docs] [issue15104] Unclear language in __main__ description

Terry J. Reedy report at bugs.python.org
Sat Jun 23 08:48:10 CEST 2012

Terry J. Reedy <tjreedy at udel.edu> added the comment:

As a native speaker, I agree that the sentence, in isolation, is hardly comprehensible. The previous one is also a bit flakey.

The situation is that top-level code executes in a module named __main__, which has one joint global/local namespace that is the global namespace for all subsidiary contexts. '__main__':<__main__ module> is added to sys.modules before user code is executed. The name __main__ is not normally in the __main__ (global) namespace, hence the comment about 'anonymous' in the first sentence. (It is not anonymous in sys.modules.) However (1) __main__ or any other module/namespace can 'import __main__' and get the reference to __main__ from sys.modules and (2) __main__ does have name __name__ bound to the *string* '__main__'. Hence a module can discover whether or not it *is* the __main__ module.

Part of the quoting confusion is that unquoted names in code become strings in namespace dicts, and hence quoted literals when referring to them as keys. What I did not realize until just now is that the __name__ attribute of a module *is* its name (key) in the module namespace (sys.modules dict). For instance, after 'import x.y' or 'from x import y', x.y.__name__ or y.__name is 'x.y' and that is its name (key) in sys.modules. So it appears that the __name__ of a package (sub)module is never just the filename (which I expected), and "__name__ is the module name" only if one considers the package name as part of the module name (which I did not).

The only non-capi reference to module.__name__ in the index is

3.2. The standard type hierarchy
"__name__ is the module’s name"

But what is the modules name? Its name in sys.modules, which is either __main__ or the full dotted name for modules in packages (as I just learned). Perhaps this could be explained better here.

nosy: +terry.reedy

Python tracker <report at bugs.python.org>

More information about the docs mailing list