471,119 Members | 1,109 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,119 software developers and data experts.

Wrap a dictionary in a class?

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....8455aa8dbceb8a

Mar 22 '06 #1
2 2267
> In another thread, it was recommended that I wrap a dictionary in a
class.
How do I do so?


I guess this is what you want:

http://vsbabu.org/mt/archives/2003/0...tionaries.html

HTH,
Daniel
Mar 22 '06 #2
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....8455aa8dbceb8a

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

Mar 22 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by none | last post: by
4 posts views Thread by brianobush | last post: by
2 posts views Thread by jg | last post: by
1 post views Thread by john wright | last post: by
4 posts views Thread by NullQwerty | last post: by
5 posts views Thread by pamela fluente | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.