classes vs dicts
Terry Reedy
tjreedy at udel.edu
Thu May 13 13:33:11 EDT 2004
"David MacQuigg" <dmq at gain.com> wrote in message
news:nj57a01tqsvtufcur2gqu3enbjll5l95id at 4ax.com...
> My problem is similar, with the additional requirement that I need a
> convenient way to access data deep within a hierarchy of parameters.
> I chose classes over dictionaries because the syntax to access items
> in a deeply nested dictionary is awkward.
>
> dict[window1][plot2][xaxis][label][font][size] = 12
>
> vs
>
> statefiles.window1.plot2.xaxis.label.font.size = 12
>
> The problem is I can't easily save the whole hierarchy to disk.
> Pickle doesn't work with classes. Also, I worry about the overhead of
> classes when I am just needing a simple container. A typical
> statefile will have 1000 parameters in 300 classes nested up to ten
> levels deep. As a structure of nested classes this takes about 74KB
> on disk. Importing the file creates a .pyc file that is 157KB !! It
> does seem to import quickly, however, so speed may not be a problem.
I believe you could write a class whose instances would correspond to
(wrap) a dictionary with possible subdicts. It would have a getattribute
method that turned the attribute name into a dict key and either return a
new instance or a 'raw' object depending on whether the corresponding value
was a nested dict or something else. You would start with one instance
representing the top level dict and work down from there. I'll leave
implementation to you.
I also believe you could instead do this as a subclass of dict itself. I
might have even seen such somewhere.
Terry J. Reedy
More information about the Python-list
mailing list