Problem in designing a global directory in python

Tian wangtianthu at gmail.com
Tue Mar 29 12:50:46 EST 2005


I want to create a object directory called Context in my program, which
is based on a dict to save and retrieve values/objects by string-type
name. I have the definition like this:

utils.py
--------------------
global sysctx

class Context:
    def __init__(self):
    def set(self, name, obj, overwrite=True):
    def get(self, name):
    def has(self, name):

def init():
    global sysctx
    sysctx = Context()

def getContext():
    global sysctx
    return sysctx
---------------------

init() is called somewhere at the beginning of the program.
In other modules, i want to use this in the following manner,

   from utils import *
   getContext().set(...)

but SOMETIMES I met following error located in "getContext()"
         NameError: global name 'sysctx' is not defined

I found that when a module is in the same directory as utils.py, when I
can simply use "utils" for importing, there is no such problem. But
when i was writing a module in a deeper directory than utils.py, which
has to use the full module name for importing, such as:

   from myproj.utils import *
   getContext().set(...)

I got this error.

What should I do to correct that?




More information about the Python-list mailing list