how to create/ref globals in an alternate namespace?

James Stroud jstroud at mbi.ucla.edu
Fri Apr 27 23:07:17 CEST 2007


Steven W. Orr wrote:
> I have two seperate modules doing factory stuff which each have the 
> similar function2:
> 
> In the ds101 module, def DS101CLASS(mname,data):
>     cname = mname+'DS101'
>     msg_class = globals()[cname]
>     msg = msg_class(data)
>     return msg
> 
> and in the fdu module,
> 
> def FDUCLASS(mname,data):
>     cname = mname+'FDU'
>     msg_class = globals()[cname]
>     msg = msg_class(data)
>     return msg
> 
> I was thinking I'd be clever and create a common function:
> def procCLASS(mname, objname, data):
>     cname = mname+objname
>     msg_class = globals()[cname]
>     msg = msg_class(data)
>     return msg
> 
> but the call to globals fouls it all up. Is there a way to write it so 
> that the call to globals can be parameterized to be in the context of a 
> specific module?
> 
> Also, I need to go the other way, a la,
>         globals()[name] = nclass
> 
> Is this doable?
> 
> TIA
> 

Why do you need all of the msg_class(es) global? Why not put them into a 
module and import the module where you need them? This would be the 
conventional way to avoid such problems.



More information about the Python-list mailing list