My solution (which will be in 1.0) is to place __name__ in the
module's dictionary, rather than __self__. This is what you want
anyway, but more importantly, the problem with inserting __self__ is
that the module and its dictionary would contain circular references
to each other which would prevent them from being garbage-collected.
Not a great concern for most programs, but it *is* possible to
entirely delete a module and cause it to be gc'ed.
Note that using __name__ you can still get at the module if desired,
through sys.modules[__name__]. Also note that __name__ can be
modified to rename a module (it is used internally in those few places
where a module's name is needed, e.g. for repr()) so you can
effectively rename a module by moving it in sys.modules and updating
its __name__ attribute.
> < dict2insert( m->md_dict, newstringobject("__self__"), m );
> (1) DO I need to have an INCREF( m ) along with that line above ?
No, because dict2insert() already does that.
> (2) ...
> So, my solution here was to check if sys.argv contains the
> module name in it's basename. ( Which, to avoid hardwiring the
> module's filename into the module, requires a way to access
> module.__name__ from within module.
I am very suspicious of modules that try to automatically figure out
if they are supposed to be the main program or not. But many people
seem to want this, so I won't stop you :-)
> (3) Does anyone have another way of getting the module name ?
I don't think there is.
> (4) Does anyone have another suggestion of a different sys.argv
> processing convention that would bypass my need for this trick ?
> Anyone else have any typical default actions on import ?
> ( whether or not they solve this particular problem. )
Possibly check if sys.argv ends in .py?
> (5) I'm still not completely wild about sticking "__self__" in
> every module's __dict__. Other ideas ? Maybe __self__ should
> be a special symbol to the parser, that is evaluated in
> context to a particular value. ( As in some other OO languages,
> where 'self' is a reserved word, rather than a conventional name. )
I presume my solution of putting __name__ there would answer this...
--Guido van Rossum, CWI, Amsterdam <Guido.van.Rossum@cwi.nl>