Joseph Turian wrote:
> In another thread, it was recommended that I wrap a dictionary in a
> class.
> How do I do so?
> 
>    Joseph
> 
> that thread:
> http://groups.google.com/group/comp.lang.python/browse_frm/thread/9a0fbdca450469a1/b18455aa8dbceb8a?q=turian&rnum=1#b18455aa8dbceb8a
> 
Perhaps like this?

 >>> adict = dict(a=1,b=2,c=3)
 >>> class Bunch(object):
...     def __init__(self, other):
...         self.__dict__ = other
...
 >>> b = Bunch(adict)
 >>> b.a
1
 >>> b.b
2
 >>> b.c
3
 >>> b.c= 42
 >>> adict
{'a': 1, 'c': 42, 'b': 2}
 >>>

Be careful: attribute access to the dictionary works only if the keys are valid 
identifiers, and not special names (which Python looks up in the class).


HTH
Michael

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to