private variables/methods

Terry Reedy tjreedy at udel.edu
Sun Oct 12 01:42:10 EDT 2003


"Jordan Krushen" <jordan at krushen.com> wrote in message
news:oprwu4uvju5ctagx at shawnews...
> I'm rather amused at the thought of a module importing itself.  I
find it
> cleaner than global, and it also emphasizes that modules are
singletons,
> if you think about how a module *can* import itself.

You can already do this in the main module:

>>> __main__
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
NameError: name '__main__' is not defined
>>> __name__
'__main__'
>>> import __main__
>>> __main__
<module '__main__' (built-in)>
>>> a=3
>>> __main__.__dict__['a']
3
>>> __main__.__dict__['b'] = 5
>>> b
5

>>> del __main__
>>> __main__
NameError: name '__main__' is not defined
>>> 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.

> I think that __current_module__ is perhaps a bit too lengthy

and redundant ;-)

Terry J. Reedy






More information about the Python-list mailing list