combining namespaces when importing two modules

Peter Hansen peter at engcorp.com
Mon Aug 22 17:04:59 EDT 2005


Donnal Walter wrote:
> I would like to be able to write something like:
> 
> import dcw as dw
> import xyz as dw
> 
> such that the 'dw' namespace includes definitions from both dcw and xyz, 
> but in the script above names from dcw1 are lost. How can I combine the 
> two? (I'd rather not use 'import *'.) Thanks.

This sounds really gross and dangerous, but you could do this, and it 
might even work.  I wouldn't recommend it, but without knowing your use 
case I can't say what I'd recommend instead.

import dcw as dw
import xyz as _
dw.__dict__.update(_.__dict__)
del _


-Peter



More information about the Python-list mailing list