Customizing module globals

Terry Reedy tjreedy at home.com
Fri Sep 7 11:31:37 EDT 2001


" Kerim Borchaev ( WarKiD ) " <warkid at storm.ru> wrote in message
news:mailman.999849010.17622.python-list at python.org...
> Thursday, September 06, 2001, 11:11:43 PM, Terry Reedy answered my
> question:
> >> Can I specify what module's to be imported symbol table will be?
> >>
> >> Say I have a module named Module that assumes that there's an
object
> >> Object without implicit declaration like this :
> >>
> >> Module.py
> >> ----------------
> >> Object.doThings()
> >> ----------------
> >>
> >> And I can import/run this module with specific a Object?
>
> TR> You can set module attributes in the obvious manner.
>
> TR> import module
> TR> module.Object = <some object>
>
> But te problem is that import fails...
>
>   File "ModuleUser.py", line 1, in ?
>     import Module
>   File "Module.py", line 1, in ?
>     Object.doThings()
>   NameError: name 'Object' is not defined

OK, You can't modify a module until it has been imported successfully,
and you cannot run something during import that has not yet been
defined.  So module would have to import Object from somewhere else
before it tries to access Object.

Terry J. Reedy






More information about the Python-list mailing list