Problem in designing a global directory in python

Bruno Desthuilliers bdesth.quelquechose at
Tue Mar 29 21:29:48 CEST 2005

Tian a écrit :
> 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:
> --------------------
> 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

Why using a (not so) global variable here ? If your problem is to make 
sure you have only one instance of Context in your program, there are 
cleaner solutions - one of them being a Singleton (you'll find all 
needed doc and exemples on the net - google is your friend !-).

More information about the Python-list mailing list