Newbie namespace question

Nick Coghlan ncoghlan at iinet.net.au
Thu Dec 23 08:39:59 CET 2004


Brandon wrote:
> Peter,
> 
> You're correct about the bug.  I did need a 'self' parm...  I was just
> winging the example because the actual code is pretty large.  I'm using
> google groups for my posting and it didn't carry spaces through (I did
> use spaces and not tabs).
> 
> The "fix" or workaround was to import __builtin__ and add the
> AdminConfig reference there in configure_server_foo.py as follows:
> 
> import __builtin__
> __builtin__.AdminConfig = AdminConfig
> 
> As for the indentations, substitute ~ with a space.
> 
> Hopefully, a bug free and "indented" version.  :)
> 
> #jdbc.py
> class DataSource:
> ~~~def __init__(self, servername):
> ~~~~~~self.servername = servername
> 
> ~~~def create(self, name, connectionInfo, etc):
> ~~~~~~#Call the IBM supplied WebSphere config object
> ~~~~~~AdminConfig.create('DataSource')
> 

Is there any particular reason DataSource can't be modified to accept a 
reference to the AdminConfig as a constructor argument? Or as a class attribute?

Alternatively, here's another trick to 'nicely' set a module global from outside 
a module:

#jdbc.py
def setAdminConfig(adm_cfg):
   global AdminConfig
   AdminConfig = adm_cfg

However, if you would prefer not to alter jdbc.py, consider trying:

import jdbc
jdbc.AdminConfig = AdminConfig


Global variables are bad karma to start with, and monkeying with __builtin__ is 
even worse :)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at email.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://boredomandlaziness.skystorm.net



More information about the Python-list mailing list