[Tutor] Benefit/Disadvantage to storing data as dictionary vs. in a class

Steven D'Aprano steve at pearwood.info
Sat Jun 2 04:12:59 CEST 2012


Adam wrote:
> I'm working on a class that handles multiple rooms that generate a large 
> amount of data. Currently my class model looks something like this (more 
> apologies for any indentation errors):

In my experience, what people consider "a large amount of data" and what the 
computer considers large is often very, very different.

You talk about "multiple rooms" with 24 records per day (one per hour). If 
each room has 100 readings per hour, and 100 outside temperatures each with 
100 readings, I would expect you to need under 50 MB per room. That's not that 
much these days.


> My question is in regards to the storing all this data in a dictionary, 
> which makes it easy to reference self.rooms[z][12][85][AVG]; however is 
> this better/worse or the same as creating a 'Room' class to store the 
> data? Since a 'Room' only contains a large amount of data, but doesn't 
> contain any functions, which form of storing the data is considered 
> 'better'?

In modern object-oriented languages like Python, the fact that a class doesn't 
contain any methods (functions) is not a problem. Objects can be used as the 
equivalent of Pascal records or C structs too.

Besides, Python dicts and lists are already objects. The amount of overhead in 
putting them together into programmer-friendly classes with named attributes 
is trivial. Assuming my figure of 50 MB above is accurate, I'd expect the 
equivalent figure for a class-based solution would be 50.2 MB. Big deal.

Write your code to make it is easy and simple to read and write as possible. 
Program as if the next guy who maintains the program will be a violent 
psychopath who knows where you live.

Your aim is to have code that is so simple that it is obviously correct, 
rather than so complicated that there are no obvious bugs. Expressions like:

     self.rooms[z][12][85][AVG]

strike me as tending towards the later ("no obvious bugs") than the first.



-- 
Steven



More information about the Tutor mailing list