classes vs dicts

David MacQuigg dmq at gain.com
Thu May 13 12:06:45 EDT 2004


On 6 May 2004 03:12:15 -0700, charlvj at yahoo.com (Charlie) wrote:

>Greetings,
>
>I am pretty new to Python and like it very much, but there is one
>thing I can't figure out and I couldn't really find anything in the
>docs that addresses this.
>
>Say I want to write an address book program, what is the best way to
>define a person (and the like): create a class (as I would do in Java)
>or use a dictionary?
>I guess using dictionaries is fastest and easiest, but is this
>recommended?

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.

Seems like Python could use a "container" structure which would be
like a class, but without the overhead and with the ability to
"pickle" the whole structure.

# ../CDP/Statefiles/bignest.py -- testfile with 960 parameters.

c960:
    c480a:
        c120a:
            c30a:
                c10a:
                    analyses:
                        transient:
                            start = '10u'
                            stop  = '20u'
                            method = 'euler'
                            readic = 'final50u'
#   .   .   .   .   .   .   .   .   .   .   .   .   .   .
                    wavescan:
                        window1:
                            xaxis:
                                label:
                                    text = "Frequency (MHz)"
                                    position = -0.5
                                    font:
                                        size   =  12
                                        family = 'arial'
                                        color  = 'blue'
                                        bold   =  True
#   .   .   .   .   .   .   .   .   .   .   .   .   .   .
                c10b:
                    analyses:
                        transient:
                            start = '10u'
                            stop  = '20u'
                            methd = 'euler'
                            readic = 'final50u'

-- Dave




More information about the Python-list mailing list