private variables/methods

Alex Martelli aleaxit at yahoo.com
Sun Oct 12 11:30:48 EDT 2003


Terry Reedy wrote:
   ...
>>>> globals()[__name__] = __import__(__name__)
>>>> __main__
> <module '__main__' (built-in)>
> 
> I believe globals line above will work in imported modules, in which
> __name__ is import name (file name if from file), but have not tested
> it in such.

Yes, this will work at the global level in any module.  But it's not a 
normal import instruction, while the modified builtin __import__ I
showed does allow normal importing; and your _use_ of __import__ and 
globals() cannot bind a _local_ variable of a function to the current 
module object.


>> I think that __current_module__ is perhaps a bit too lengthy
> 
> and redundant ;-)

I disagree.  Lengthy it may be, but we do want a 'reserved module
name' to use for this purpose.  For a normal import instruction to
work, and to work just as well if you cut and paste the same function
elsewhere, I think we want to define that "import somespecificname" is 
importing THIS module, the CURRENT module, under that name (or another 
specified with 'as').  This can be experimented with easily, by changing
the builtin __import__ (or setting an import hook, maybe) in site-specific
file; and if it catches on the builtin __import__ could easily be customized
to perform the same task quite speedily.


Alex





More information about the Python-list mailing list