Problem in designing a global directory in python

Terry Reedy tjreedy at udel.edu
Tue Mar 29 21:40:49 EST 2005


"Tian" <wangtianthu at gmail.com> wrote in message 
news:1112118646.177300.24800 at g14g2000cwa.googlegroups.com...
>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

I suspect that you would accomplish your goal much more easily by calling 
your module 'context' or something like that, importing it into every other 
module that needs it, and then accessing values as attributes of context. 
Don't reinvent the module ;-)

other.py

import context

context.a = 3

x = context.b # where context.b is previous set somewhere else

If you need multiple shared contexts, people subclass object

class context(object): pass

context1 = context()
context2 = context()

# now set and get attributes as needed

Terry J. Reedy






More information about the Python-list mailing list